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());
    }
相關文章
相關標籤/搜索