Convert a byte stream to a character stream
ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.OutputStream;ImportJava.io.OutputStreamWriter;/* * InputStreamReader and OutputStreamWriter class * used to convert a byte stream to a character stream * Note: There is no way to convert a stream of characters to a byte stream in Java */ Public class Test04 { Public Static void Main(string[] args)throwsIOException {test2 (); }//Convert file byte input stream to character stream Public Static void test1()throwsIOException {//define file byte input streamInputStream is =NewFileInputStream ("D:\\java\\hello.txt");//convert it to a character streamInputStreamReader ISR =NewInputStreamReader (IS);intData while(data = Isr.read ())! =-1) {System.out.println (Char) data); } isr.close (); Is.close (); }//Convert file byte output stream to character stream Public Static void test2()throwsIOException {//define file byte output streamOutputStream OS =NewFileOutputStream ("D:\\java\\hello.txt");//convert it to a character stream, specifying the encoding to use when writingOutputStreamWriter OSW =NewOutputStreamWriter (OS,"GBK");//Converts the standard byte input stream to a character stream, specifying the encoding to use when readingInputStreamReader ISR =NewInputStreamReader (System.in,"GBK");intData System.out.println ("Please enter data:"); while((Data=isr.read ())! =' Q ') {osw.write (data); Osw.flush (); } isr.close (); Os.close (); }}
Adding buffering for character input and output streams
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.FileReader;ImportJava.io.FileWriter;ImportJava.io.IOException;ImportJava.io.InputStreamReader;/* * BufferedReader and BufferedWriter Buffer stream * Add buffer for character input/output stream */ Public class Test05 { Public Static void Main(string[] args)throwsIOException {test2 (); }//Add buffering for character input stream Public Static void test1()throwsIOException {//Convert standard byte input stream to character streamInputStreamReader ISR =NewInputStreamReader (System.in,"GBK");//Add buffering for character input streamBufferedReader br =NewBufferedReader (ISR); System.out.println ("Please enter data:"); String str =NULL; while(str = br.readline ())! =NULL) {System.out.println (str); } br.close (); Isr.close (); }//Using buffered streams to copy files Public Static void test2()throwsIOException {//Define a file character input streamFileReader FR =NewFileReader ("D:\\java\\hello.txt");//Add buffering for input streamBufferedReader br =NewBufferedReader (FR);//Define a file character output streamFileWriter FW =NewFileWriter ("D:\\java\\world.txt");//Add buffering for the output streamBufferedWriter BW =NewBufferedWriter (FW); String str =NULL; while(str = br.readline ())! =NULL) {bw.write (str); } bw.flush (); System.out.println ("File copy succeeded!" "); Bw.close (); Br.close (); }}
Java Learning Note (33)-byte stream converted to character stream