閱讀569 返回首頁    go 技術社區[雲棲]


Java IO--字節-字符流轉換OutputStreamWriter/InputStreamReader

OutputStreamWriter和InputStreamReader


一般在操作輸入輸出內容的就需要使用字節或字符流,但是有些時候需要將字符流變為字節流的形式,或者將字節流變為字符流的形式,所以,就需要另外一組轉換流的操作類。


import java.io.* ;
public class OutputStreamWriterDemo01{
	public static void main(String args[]) throws Exception	{	// 所有異常拋出
		File f = new File("d:" + File.separator + "test.txt") ;	
		Writer out = null ;	// 字符輸出流
		out = new OutputStreamWriter(new FileOutputStream(f)) ;	// 字節流變為字符流
		out.write("hello world!!") ;	// 使用字符流輸出
		out.close() ;
	}
};


import java.io.* ;
public class InputStreamReaderDemo01{
	public static void main(String args[]) throws Exception{
		File f = new File("d:" + File.separator + "test.txt") ;	
		Reader reader = null ;
		reader = new InputStreamReader(new FileInputStream(f)) ;	// 將字節流變為字符流
		char c[] = new char[1024] ;
		int len = reader.read(c) ;	// 讀取
		reader.close() ;	// 關閉
		System.out.println(new String(c,0,len)) ;
	}
};






最後更新:2017-04-03 14:54:00

  上一篇:go 【轉載】Linux(ubuntu)下如何安裝與卸載軟件總結
  下一篇:go spring如何在xml裏配置Calendar,Date