1. 程式人生 > >java IO流 讀取檔案 && 檔案複製

java IO流 讀取檔案 && 檔案複製

用InputStream讀取檔案

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class TestInputStream {
	public static void main(String args[]){
		int b = 0;
		FileInputStream in = null;
		try{
			in = new FileInputStream("D:\\androidPractice\\J2SE\\src\\com\\why\\TestInputStream.java");
		}catch(FileNotFoundException e){
			System.out.println("cannot find the file!");
			System.exit(-1);
		}
		
		try{
			long num = 0;
			while((b=in.read())!= -1){
				System.out.print((char)b);
				num++;
			}
			in.close();
			System.out.println();
			System.out.println("一共讀取了"+num+"個字元");
		}catch(IOException e1){
			System.out.println("檔案讀取錯誤!!");
			System.exit(-1);
		}
	}

}
結果如圖:

運用OutputStream 複製某檔案內容到另一檔案:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestOutputStream {
	public static void main(String args[]){
		int b = 0;
		FileInputStream in = null;
		FileOutputStream out = null;
		try{
		in = new FileInputStream("D:\\androidPractice\\J2SE\\src\\com\\why\\TestInputStream.java"); 
		out = new FileOutputStream("D:\\androidPractice\\J2SE\\src\\com\\why\\TestInputStream.txt");
		while((b = in.read()) != -1){
			out.write(b);
		}
		in.close();
		out.close();
		}catch(FileNotFoundException e1){
			System.out.println("file is not found!");
			System.exit(-1);
		}catch(IOException e2){
			System.out.println("檔案複製錯誤!!");
			System.exit(-1);
		}
		System.out.println("檔案複製成功鳥~~!");
	}

}
結果可以看到,程式碼複製到了TXT檔案中:

用filereader讀取檔案  這時讀取的是字元,用inputstream讀取的是位元組 ,所以那個讀取的有一串問號“??????”  這些問號的地方是中文,中文是兩個位元組,而inputstream

每次讀取的是一個字元,所以讀不出來完整的字元,顯示問號。

用filereader讀取檔案  示例:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class TestFileReader {
	public static void main(String args[]){
		FileReader fr = null;
		int n = 0;
		try{
			fr = new FileReader("D:\\androidPractice\\J2SE\\src\\com\\why\\TestFileReader.java");
			while((n = fr.read()) != -1){
				System.out.print((char)n);  //一定不要忘記型別轉換,這裡不要換行  不要ln了~~!
			}	
			fr.close();  //一定不要忘記close !!close,close!!!
		}catch(FileNotFoundException e1){
			System.out.println("找不到指定檔案!");
		}catch(IOException e2){
			System.out.println("檔案讀取錯誤!!");
		}
	}

}
讀取結果可以看到,中文也讀了出來:

使用Print流  往指定檔案 寫資料


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class TestPrintStream {
	public static void main(String args[]){
		PrintStream ps = null;
		try{
			FileOutputStream fos = new FileOutputStream("e:\\a.txt");
			ps = new PrintStream(fos);
		}catch(IOException e){
			e.printStackTrace();
		}
		if(ps != null){
			System.setOut(ps);
		}
		int ln = 0;
		for(char n=0; n<20000; n++){
			System.out.print(n+" ");
			if(ln++ >= 100){System.out.println(); ln = 0;}
		}
	}

}

可以看見E盤生成了a.txt,開啟檔案,如下圖所示:


運用邏輯思維,想象力 想象各種管道。

使用object流:



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class TestObjectIO {
	public static void main(String args[]) throws Exception{
		T t = new T();
		t.c = 8;
		FileOutputStream fos = new FileOutputStream("e:\\a.txt");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(t);
		oos.flush();
		oos.close();
		
		FileInputStream fis = new FileInputStream("e:\\a.txt");
		ObjectInputStream ois = new ObjectInputStream(fis);
		T tReaded = (T)ois.readObject();
		System.out.println("tReaded.a"+tReaded.a+" "+"tReaded.b"+tReaded.b+" "+"tReaded.c"+tReaded.c+" "+"tReaded.d"+tReaded.d+" ");
	}

}
class T implements Serializable{
	int a = 1;
	int b = 2;
	int c = 3;
	double d = 4.5;
}
控制檯輸出:



但是開啟E盤的a.txt檔案 是亂碼。。。


不知道怎麼解決。。待續。。