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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值