Java中File、字節流

File案例

需求:從控制檯輸入一個字符串 該字符串是文件夾路徑 計算這個文件夾的大小

public static File getDirFile() {
    Scanner scanner = new Scanner(System.in);
    while(true) {
        System.out.println("請輸入一個文件夾路徑:");
        String path = scanner.nextLine();
        File file = new File(path);
        if (!file.exists()) {
            System.out.println("你輸入的路徑不存在,請重新輸入");
        }else if (file.isFile()) {
            System.out.println("你輸入的是個文件,請重新輸入");
        }else {
            return file;
        }
    }
}

計算文件夾的大小

public static long getDirLengthByFile(File file) {
    long sum = 0;
    File[] listFiles = file.listFiles();
    for (File subF : listFiles) {
        if (subF.isFile()) {
            sum = sum + subF.length();
        }else {
            sum = sum + getDirLengthByFile(subF);
        }           
    }
    return sum;
}
/*
 * 測試
 */
public static void fun() {
    File file = getDirFile();
    long length = getDirLengthByFile(file);
    System.out.println(length);
}

需求:從控制檯輸入一個字符串 該字符串是文件夾路徑 刪除這個文件夾

/*
 * 根據文件夾路徑刪除這個文件夾
 */
public static void deleteDir(File file) {
    File[] listFiles = file.listFiles();
    for (File subFile : listFiles) {
        if (subFile.isFile()) {
            subFile.delete();
        }else {
            deleteDir(subFile);
        }           
    }
    file.delete();
}
/*
 * 測試
 */
public static void fun() {
    File file = getDirFile();
    deleteDir(file);
}
字節流
參照物:該執行程序。
輸出(OutPut)定義:程序到文件的輸出過程
輸入(InPut)定義:文件到程序的輸入過程

OutputStream(字節輸出流):

是個抽象類 是所有輸出流的父類

OutputStream的方法:

/*
 * 三種寫入方法
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileOutputStream oStream = new FileOutputStream(file);
    //  寫入數據
    oStream.write(49);
    //  寫入字節數組
    byte[] b = {65, 66, 67, 68};
    oStream.write(b);
    //  字節數組的索引和長度寫入
    oStream.write(b, 1, 2);
    oStream.close();
}
/*
 * 文件的續寫和換行
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileOutputStream oStream = new FileOutputStream(file, true);
    oStream.write("holle\n".getBytes());
    oStream.close();
}
/*
 * 異常處理(手動)
 */
public static void fun() {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FlieOutPutStream oStream = null;
    try {
        oStream = new  FlieOutPutStream(file);
        oStream.write("holle".getBytes());
    } catch (FileNotFoundException e) {
        throw new RuntimeException("文件找不到");
    } catch (IOException e) {
        throw new RuntimeException("文件編寫失敗");
    } finally {
        try {
            if (oStream != null) {
                oStream.close();
            }
        } catch (IOException e) {
            throw new RuntimeException("關閉失敗");
        }
    }
}
InputStream(字節輸入流):

1.是所有輸入流的父類
2.注意:字節流寫入的是一個字節一個字節寫,讀取也是一個字節一個字節的讀
InputStream的方法:
/*
 * 讀取方法
 * 注意:當讀取到文件末尾時 會返回-1(相當於讀取結束)
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileInputStream iStream = new FileInputStream(file);
    int i1 = iStream.read();
    System.out.println((char)i1);       
    i1 = iStream.read();
    System.out.println((char)i1);       
    i1 = iStream.read();
    System.out.println(i1);     
    i1 = iStream.read();
    System.out.println(i1);
    iStream.close();
}
/*
 * 循環讀
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileInputStream iStream = new FileInputStream(file);
    int read = 0;
    while((read = iStream.read()) != -1) {
        System.out.println((char)read);
    }
    iStream.close();
}
/*
 * 使用字節數組 讀取文件
 */
public static void fun() throws IOException {
    File file = new File("/Users/lanou/Desktop/Test/jian.txt");
    FileInputStream iStream = new FileInputStream(file);
    byte[] b = new byte[1024];
    int i1 = iStream.read(b);
    System.out.println(i1); // 2
    System.out.println(new String(b));// ab
    iStream.close();
}










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章