1. 程式人生 > >java學習筆記(9-IO流)

java學習筆記(9-IO流)

1.FileWriter

package daigua8;
import java.io.FileWriter;
import java.io.IOException;
/*
輸出流寫資料的步驟:
*       A:建立輸出流物件
*       B:呼叫輸出流物件的寫資料的方法
*       C:釋放資源
*/
public class FileWriterDemo {
    public static void main(String[] args) throws IOException {
        // 建立一個輸出流物件
        FileWriter fw = new FileWriter("/Users/dengrunting/Desktop/test.txt");
        // 寫入資訊到檔案
        fw.write("你好,我是你爸爸!");
        // 從記憶體緩衝區,壓入到磁碟
        fw.flush();
        // 關閉輸出流物件,系統釋放相關資源
        fw.close();
    }
}

2.FileReader

package daigua8;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderDemo {
    public static void main(String[] args) throws IOException{
        // 建立一個輸入流物件
        FileReader fr = new FileReader("/Users/dengrunting/Documents/eclipse-workspace/my_demo/src/daigua8/FileReaderDemo.java");
        int ch;
        while ((ch = fr.read()) != -1) {
            System.out.print((char)ch);
        }
        fr.close();
    }
}

3.檔案複製(單位元組)

package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;

public class FileCopy {
    public static void main(String[] args) throws IOException{
        // 建立輸入輸出流物件
        FileReader fr = new FileReader("/Users/dengrunting/Desktop/java視訊/test.txt");
        FileWriter fw = new FileWriter("/Users/dengrunting/Desktop/java視訊/test_copy.txt");
        
        // 邊讀邊寫
        int ch;
        while ((ch = fr.read()) != -1) {
            System.out.print((char)ch);
            fw.write((char)ch);
        }
        // 重新整理緩衝區
        fw.flush();
        
        fw.close();
        fr.close();
    }

}

4.檔案複製(多位元組)

package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;

public class FileCopy2 {
    public static void main(String[] args) throws IOException{
        // 建立輸入輸出流物件
        FileReader fr = new FileReader("./src/com/daigua/FileCopy.java");
        FileWriter fw = new FileWriter("./src/com/daigua/copy.FileCopy.java");
        
        // 邊讀邊寫,一次讀入1024位元組
        char[] chs = new char[1024];
        int ch;
        while ((ch = fr.read(chs)) != -1) {
            System.out.print(chs);
            fw.write((char)ch);
        }
        // 重新整理緩衝區
        fw.flush();
        
        fw.close();
        fr.close();
    }

}

5.緩衝流複製檔案

package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

public class BufferedReaderDemo {
    public static void main(String[] args) throws IOException{
        // 建立輸入輸出流物件
        BufferedReader fr = new BufferedReader(new FileReader ("./src/com/daigua/BufferedReaderDemo.java"));
        BufferedWriter fw = new BufferedWriter(new FileWriter("./src/com/daigua/copy.FileCopy2.java"));
        
        // 邊讀邊寫,一次讀入1024位元組
        char[] chs = new char[1024];
        int len;
        while ((len = fr.read(chs)) != -1) {
            System.out.print(chs);
            fw.write(chs, 0, len);
        }
        // 重新整理緩衝區
        fw.flush();
        
        fw.close();
        fr.close();
    }

}

6.緩衝流特有方法複製檔案

package com.daigua;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;

public class BufferedStreamDemo {
    public static void main(String[] args) throws IOException{
        // 建立輸入輸出流物件
        BufferedReader fr = new BufferedReader(new FileReader ("./src/com/daigua/BufferedReaderDemo.java"));
        BufferedWriter fw = new BufferedWriter(new FileWriter("./src/com/daigua/copy.FileCopy3.java"));
        
        // 邊讀邊寫,以此讀取一行
        String line;
        while ((line = fr.readLine()) != null) {
            System.out.print(line + "\r\n");
//          fw.write(line + "\r\n");
            fw.write(line);
            fw.newLine();
            fw.flush();  // 讀一行寫一行
        }
        
        fw.close();
        fr.close();
    }

}