天天看點

Java學習路線-25:檔案操作File類第15 章 : 檔案操作

第15 章 : 檔案操作

67 File類基本操作

檔案作業系統操作類:

java.io.File

檔案建立,删除,重命名

File類基本使用

File 實作了Comparable接口

// 構造方法
public File(String pathname)
public File(String parent, String child)

// 建立檔案
public boolean createNewFile() throws IOException

// 檔案存在
public boolean exists()

// 删除檔案
public boolean delete()      

示例:

import java.io.File;
import java.io.IOException;

class Demo {
    public static void main(String[] args) throws IOException {
        File file = new File("./demo.txt");

        if(file.exists()){
            boolean ret = file.delete();
            System.out.println("删除結果:" + ret);

        } else{
            boolean ret =  file.createNewFile();
            System.out.println("建立結果:" + ret);
        }
    }
}      

68 File類操作深入

1、路徑分隔符

Windows分隔符 

"\"

Linux分隔符 

"/"

路徑分隔符常量 File.separator

2、檔案操作流程

程式 -> JVM -> 作業系統函數 -> 檔案處理      

重複删除或建立的時候會有延時情況,

建議:檔案名不要重名

3、建立檔案的時候父級目錄必須存在

// 擷取父路徑
public File getParentFie()

// 建立目錄
public boolean mkdirs()      

示例

import java.io.File;
import java.io.IOException;

class Demo {
    public static void main(String[] args) throws IOException {
        File file = new File("./dir/demo/demo.txt");
        File parentFile = file.getParentFile();

        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }

        file.createNewFile();
    }
}      

69 擷取檔案資訊

涉及檔案本身操作,不涉及檔案内容處理

// 是否可讀
public boolean canRead()

// 是否可寫
public boolean canWrite()

// 是否可執行
public boolean canExecute()

// 是否為檔案
public boolean isFile()

// 是否為目錄
public boolean isDirectory()

// 擷取檔案長度 位元組
public long length()

// 最後修改時間 13位時間戳
public long lastModified()

// 列出目錄内容
public File[] listFiles()

      

70 綜合案例:列出目錄結構

import java.io.File;

class Demo {
    public static void main(String[] args) {
        File file = new File("./dir");
        listDir(file);
    }

    public static void listDir(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            
            if(files != null){
                for(File f: files){
                    listDir(f);
                }
            }
        } else{
            System.out.println(file);
        }
    }
}      

71 綜合案例:檔案批量更名

import java.io.File;

class Demo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();

        File file = new File("./dir");
        renameDir(file);

        long end = System.currentTimeMillis();
        System.out.println("花費時間:" + (end - start));
    }

    public static void renameDir(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();

            if(files != null){
                for(File f: files){
                    renameDir(f);
                }
            }
        } else{

            if (file.getName().contains(".")){
                int endPos = file.getName().lastIndexOf(".");
                String name = file.getName().substring(0, endPos);

                File newFile = new File(file.getParent(), name + ".txt");

                System.out.println(file.getName());
                System.out.println(newFile);
                
                file.renameTo(newFile);
            }
        }
    }
}