學習java的Io操作(2),往檔案中寫入內容,讀取檔案中的內容。
package com.dufy.io;import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.Writer;/** * * 練習IO操作 second <br/> * 代碼是寫出來的,不是看出來的,我是阿飛-aflyun <br/> * 在路上。 * @author aflyun * @email 742981086@qq.com * */public class TestSecondIo {public static void main(String[] args) throws IOException {writeByteFile();//位元組流寫檔案readByteFile();//位元組流讀檔案changeReadByteFile();//修改位元組流讀檔案writeStringFile();//字元流寫檔案readStringFile();//字元流寫檔案}/** * 位元組流 * 1:向檔案中寫入字串 * 2:向檔案中一個位元組一個位元組的寫入字串 * 3:向檔案中追加新內容 * @throws IOException */public static void writeByteFile() throws IOException{String path = "E:"+ File.separator +"aflyun.txt";File file = new File(path);OutputStream os = new FileOutputStream(file);String str = "Hello World!";byte[] b = str.getBytes();//1:向檔案中寫入字串os.write(b);//2:向檔案中一個位元組一個位元組的寫入字串for (int i = 0; i < b.length; i++) {os.write(b[i]);}//3:向檔案中追加新內容String appendStr = "\nI am aflyun!"; // \n 表示換行byte[] b1 = appendStr.getBytes();os.write(b1);//輸出 Hello World! I am aflyun!os.flush();os.close();}/** * 位元組流 * 1:讀取內容 * @throws IOException */public static void readByteFile() throws IOException{String path = "E:"+ File.separator +"aflyun.txt";File file = new File(path);InputStream is = new FileInputStream(file);byte b[] = new byte[1024]; //設定最多存1024個位元組int len = is.read(b);is.close();System.out.println(new String(b));System.out.println("檔案讀入長度: " + len);}/** * 修改上面那種方式 * 位元組流 * 上面那種方式預先申請了一個指定大小的空間,但是有時候這個空間可能太小,有時候可能太大,我們需要準確的大小,這樣子可以更好的利用空間 * @throws IOException */public static void changeReadByteFile() throws IOException{String path = "E:"+ File.separator +"aflyun.txt";File file = new File(path);InputStream is = new FileInputStream(file);byte b[] = new byte[(int) file.length()]; //設定讀取的該檔案的長度,這樣子可以很好的設定使用的空間//附1:一個位元組一個位元組的讀/*for (int i = 0; i < b.length; i++) {b[i] = (byte) is.read();}*///附2:判斷檔案是否讀完int tmp = 0;int count = 0;while((tmp = is.read())!= -1){b[count++] = (byte) tmp;}is.close();System.out.println(new String(b));System.out.println(count);}/** * 字元流 * 檔案寫人字元資料 * @throws IOException */public static void writeStringFile() throws IOException{String path = "E:"+ File.separator +"aflyunGood.txt";File file = new File(path);Writer out = new FileWriter(file);String str = "你好,依依。";out.write(str);out.close();}/** * 字元流 * 檔案讀出 * @throws IOException */public static void readStringFile() throws IOException{String path = "E:"+ File.separator +"aflyunGood.txt";File file = new File(path);BufferedReader bf = new BufferedReader(new FileReader(file));String str = "";while((str = bf.readLine()) != null){ //按行讀System.out.println(str);}bf.close();}/** * 總結 位元組流和字元流的區別 * 位元組流在操作的時候本身是不會用到緩衝區的,是檔案本身的直接操作的,但是字元流在操作的 時候下後是會用到緩衝區的,是通過緩衝區來操作檔案的。 * 試著將上面的位元組流和字元流的程式的最後一行關閉檔案的代碼注釋掉,然後運行程式看看。你就會發現使用位元組流的話,檔案中已經存在內容,但是使用字元流的時候,檔案中還是沒有內容的,這個時候就要重新整理緩衝區。 * 那到底那個好一些呢。 * 答案是位元組流。首先因為硬碟上的所有檔案都是以位元組的形式進行傳輸或者儲存的,包括圖片等內容。但是字元只是在記憶體中才會形成的,所以在開發中,位元組流使用廣泛。 */}