zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    毕业生的商业软件开发之路 C#语言简介
    [毕业生的商业软件开发之路]第一次使用VS.NET集成开发环境
    一种应用程序命令执行架构设计
    DCWriter 电子病历文档编辑器的 电子病历功能规范对照表
    [毕业生的商业软件开发之路]积累与创新
    WEB开发人员的微软技术战略
    PureMVC(AS3)剖析:吐槽
    走在网页游戏开发的路上(九)
    [服务器开发]可伸缩系统的设计模式(译)
    回合制页游
  • 原文地址:https://www.cnblogs.com/YKang/p/7279461.html
Copyright © 2011-2022 走看看