Java IO之File

public static void main(String[] args) {

        // File对象,表示目录或文件
        File file01 = new File(".");

        // 返回构造方法传入的路径
        String currentPath = file01.getPath();
        System.out.println("getPath: " + currentPath);

        // 返回绝对路径
        String currentAbsolutePath = file01.getAbsolutePath();
        System.out.println("getAbsolutePath: " + currentAbsolutePath);

        // 它和绝对路径类似,但是返回的是规范路径
        String currentCanonicalPath = null;
        try{
            currentCanonicalPath = file01.getCanonicalPath();
            System.out.println("getCanonicalPath: " + currentCanonicalPath);
        }catch (IOException e){
            e.toString();
        }

        // 因为Windows和Linux的路径分隔符不同,File对象有一个静态变量用于表示当前平台的系统分隔符
        System.out.println("separator: " + File.separator);

        // 调用isFile(),判断该File对象是否是一个已存在的文件,调用isDirectory(),判断该File对象是否是一个已存在的目录
        System.out.println("isDirectory: " + file01.isDirectory());
        System.out.println("isFile: " + file01.isFile());


        File file02 = new File(currentCanonicalPath + File.separator + "file2.txt");
        System.out.println("canRead: " + file02.canRead());
        System.out.println("canWrite: " + file02.canWrite());
        System.out.println("canExecute: " + file02.canExecute());
        System.out.println("length: " + file02.length());


        // 创建和删除文件
        File file03 = new File(currentCanonicalPath + File.separator + "file3.txt");
        try{
            boolean isCreate = file03.createNewFile(); // 文件已存在时返回false
            System.out.println("isCreate: " + isCreate);

            if(isCreate){
                System.out.println("create successful");

                if(file03.delete()){
                    System.out.println("delete successful");
                }
            }
        }catch (IOException e){
            System.out.println("文件创建和删除失败!");
        }


        // 有些时候,程序需要读写一些临时文件,File对象提供了createTempFile()来创建一个临时文件,以及deleteOnExit()在JVM退出时自动删除该文件
        try{
            File file04 = File.createTempFile("tmp-", ".txt");
            file04.deleteOnExit();
            System.out.println("isFile: " + file04.isFile());
            System.out.println("getAbsolutePath: " + file04.getAbsolutePath());
        }catch (IOException e){
            System.out.println("create temporary file failed");
        }


        // 遍历文件和目录
        File file05 = new File("C:\\Windows");
        File[] files = file05.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".exe");
            }
        });
        for (File f : files){
            System.out.println("filename: " + f.getName());
        }

        // 和文件操作类似,File对象如果表示一个目录,可以通过以下方法创建和删除目录
        File file06 = new File(currentCanonicalPath + File.separator + "dir");
        File file07 = new File(currentCanonicalPath + File.separator + "dir1" + File.separator + "dir2");
        boolean isMkDir = file06.mkdir();
        boolean isMkDirs = file07.mkdirs();
        System.out.println("isMkDir: " + isMkDir); // 目录已存在返回false
        System.out.println("isMkDirs: " + isMkDirs);

        boolean deleteDir = file06.delete(); // 目录为空才能删除,目录不存在返回false
        boolean deleteDirs = file07.delete();
        System.out.println("deleteDir: " + deleteDir);
        System.out.println("deleteDirs: " + deleteDirs);


        // Path
        Path path01 = Paths.get(".", "project", "study");
        System.out.println("Path: " + path01); // Path: .\project\study
        System.out.println("AbsolutePath: " + path01.toAbsolutePath()); // AbsolutePath: D:\Code\Java\java\.\project\study
        System.out.println("normalizePath: " + path01.toAbsolutePath().normalize()); // normalizePath: D:\Code\Java\java\project\study
        System.out.println("toFile: " + path01.toFile());
    }
上一篇:设置pycharm文件默认换行符onfiguring Line Separators


下一篇:如何在Apple TV上创建文件夹?