当前位置: 首页 > news >正文

java.io.File

java.io.File 是操作文件夹及文件的类,具有跨平台的特性

Constructor

  File(String pathname)

    Creates a new File instance by the given pathname string

  FIle(File parent, String child)

    Creates a new File instance from a parent abstract pathname and a child pathname string.

  File(String parent, String child)

    Creates a new File instance from a parent pathname string and a child pathname string.

        File file =  new File("E:/a.txt");
        
        File parent = new File("E:/");
        String child = "a.txt";
        file = new File(parent, child);
        
        String parentStr = "E:/";
        file = new File(parentStr, child);
        
        System.out.println(file.pathSeparator);  // ;  配置环境变量的分隔符  inux 下为 :
        System.out.println(file.separator);   // \  路径分隔符 linux下为/
        System.out.println(file);   //  E:\a.txt
View Code

Method

  boolean canWrite();     // canExective()  canRead()

    Tests whether the application can read the file

  boolean exists()

    Tests whether the file(directory) is exists

  boolean createNewFile()   

    only can creat the  file not directory, if the file has exists, do nothing

  boolean delete()

    Deletes the file or directory

  String getName()

    Returns the name of the file or directory

  File getAbsoluteFile()

    Returns the absolute form of this abstract pathname

  

File file =  new File("E:/a.txt");
        
boolean b = file.exists();   // 判断文件/文件夹是否存在
b = file.canExecute();       // 判断文件/文件夹是否可执行
b = file.canRead();            // 判断文件是否可读
        
b = file.createNewFile();       // 文件不存在则创建,存在什么都不做
b = file.delete();     // 删除文件/文件夹
        
File newFile  = file.getAbsoluteFile();   //  E:\a.txt 得到绝对路径对象
String name = file.getName();   // E:\a.txt 得到文件名/文件夹名  即最后一级目录
View Code

  String getParent()

    Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory

  boolean isDirectory()

    Tests whether the file denoted by this abstract pathname is a directory.

  long length()

    Returns the length of the file

  File[] listFiles()

    Returns an array of abstract pathnames

File file =  new File("E:\\usefulPY");
        
String parent = file.getParent();  // E:\ 
boolean b = file.isDirectory();   // 判断是否是目录
long l = file.length();    // 获取文件(只能是文件)大小   
File[] fileList = file.listFiles(); // 获取此目录下文件夹及文件
//  [E:/usefulPY/1.txt,E:/usefulPY/voa.py]
View Code

  File[]  listFiles(FileFilter filter)  //  FileFilter    This is a functional interface 

                File file =  new File("E:\\usefulPY");
        
        
        // 匿名内部类实现方法
        File[] fileList = file.listFiles(new FileFilter(){

            @Override
            public boolean accept(File pathname) {
                String path  = pathname.getName();
                return path.endsWith(".txt");
            }
            
        }); // 获取此目录下文件夹及文件     [E:/usefulPY/1.txt] 
        
        // lambda 表达式
        File[] fileList = file.listFiles(pathname-> {
            String path  = pathname.getName();
            return path.endsWith(".txt");
        } );
        
View Code

  boolean mkdirs()  // can creat muti-level directory

    Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.

  boolean renameTo(File file)  

boolean b = file.renameTo(new File("E:/b.txt"));  // 重命名文件或文件夹
b = file.setWritable(false);   // 修改权限

File file =  new File("E:/aa/a/a");    
b = file.mkdirs();    //  E:\aa\a\a

recursive the directory

public static void main(String[] args) {
        
        ArrayList<File> fileList = recursiveFile(new File("E:\\officFile"));
        for(File f:fileList){
            System.out.println(f);
        }
    }
public static  ArrayList<File>  recursiveFile(File file){
        
        ArrayList<File> fileList =  new ArrayList<>();
        
        if(file.exists() == false)
            return fileList;
        File[] list = file.listFiles();
        for(File f: list)
        {
            if(f.isDirectory()){    
                fileList.addAll(recursiveFile(f));    
            }else{
                fileList.add(f);        
            }
            
        }
        return fileList;
 
    }
}
View Code

 

转载于:https://www.cnblogs.com/YKang/p/7279461.html

相关文章:

  • Oracle宣布提供新的Java支持价格体系
  • 第二棵树:Splay
  • MySQL之表lock信息
  • set serveroutput on
  • java基础-网络编程(Socket)技术选型入门之NIO技术
  • Java 类的加载机制
  • PHP扩展模块、Apache之rewrite模块
  • 逸管家:共享人才时代已经到来,你做好准备了吗?
  • 【阿里云大学课程】怎样学好C语言?从入门到实战
  • java笔记
  • 常用的网络营销方法有哪些
  • [BZOJ] 2427: [HAOI2010]软件安装
  • ios开发之--系统控件显示中文
  • HDU 6081 度度熊的王国战略【并查集/数据弱水题/正解最小割算法】
  • opencv2学习之threshold:实现图像阈值分割
  • Android框架之Volley
  • echarts的各种常用效果展示
  • Fundebug计费标准解释:事件数是如何定义的?
  • IndexedDB
  • iOS小技巧之UIImagePickerController实现头像选择
  • JavaScript 基本功--面试宝典
  • java中具有继承关系的类及其对象初始化顺序
  • JS实现简单的MVC模式开发小游戏
  • KMP算法及优化
  • Next.js之基础概念(二)
  • opencv python Meanshift 和 Camshift
  • windows下使用nginx调试简介
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 笨办法学C 练习34:动态数组
  • 关于 Cirru Editor 存储格式
  • 基于Android乐音识别(2)
  • 基于HAProxy的高性能缓存服务器nuster
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 你对linux中grep命令知道多少?
  • ​​​​​​​GitLab 之 GitLab-Runner 安装,配置与问题汇总
  • ​Java并发新构件之Exchanger
  • ​批处理文件中的errorlevel用法
  • ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLTr
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #我与Java虚拟机的故事#连载06:收获颇多的经典之作
  • (06)金属布线——为半导体注入生命的连接
  • (07)Hive——窗口函数详解
  • (2022 CVPR) Unbiased Teacher v2
  • (6)STL算法之转换
  • (C++17) optional的使用
  • (附源码)springboot社区居家养老互助服务管理平台 毕业设计 062027
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (图)IntelliTrace Tools 跟踪云端程序
  • (转)大型网站架构演变和知识体系
  • .net core 6 集成 elasticsearch 并 使用分词器
  • .NET构架之我见
  • .net生成的类,跨工程调用显示注释
  • @Transient注解
  • [ C++ ] STL_list 使用及其模拟实现