處理流中的轉換流。InputStreamReader & OutputStreamWriter

import java.io.*;
public class TestTransform {

	public static void main(String[] args) {
		try{
			//create a text file
			OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\sync\\Java\\TestTransform\\encode.txt"));
			osw.write("Microsoft C# vs Oracle Java");
			System.out.println(osw.getEncoding());
			osw.flush();
			osw.close();
			//read the text file
			FileReader fr = new FileReader("E:\\sync\\Java\\TestTransform\\encode.txt");
			int call = 0 ;
			while((call = fr.read()) != -1){
				System.out.print((char)call);
			}
			fr.close();
			System.out.println("\n");
			//change the Encodeing_text file
			osw = new OutputStreamWriter(new FileOutputStream("E:\\sync\\Java\\TestTransform\\encode.txt",true),"ISO-8859-1");
			osw.write(". Cuz the 2nd argument, there is an new String appended in the end.");
			System.out.println(osw.getEncoding());
			osw.flush();
			osw.close();
			//display final text file
			FileReader ffr = new FileReader("E:\\sync\\Java\\TestTransform\\encode.txt");
			while((call = ffr.read()) != -1){
				System.out.print((char)call);
			}
			ffr.close();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
	}
}

FileOutputStream(String string,boolean)的用法

OutputStreamWriter制定編碼的用法

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