• 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:aaaa

    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
    WE ARE ALL IN THE GUTTER, BUT SOME OF US ARE LOOKING AT THE STARS
  • 相关阅读:
    蓝桥杯_基础训练_龟兔赛跑预测
    大数加法
    Splay!
    topsort
    各种方法
    有时候dfs可以简化各种组合的操作
    组合数学
    重新认识三观
    手速狗还是不行啊。。。
    set和map和pair 转自ACdreamers
  • 原文地址:https://www.cnblogs.com/YKang/p/7279461.html
Copyright © 2020-2023  润新知