1 PackageCom.io;2 3 ImportJava.io.File;4 ImportJava.io.FileInputStream;5 Importjava.io.FileNotFoundException;6 Importjava.io.IOException;7 ImportJava.io.InputStream;8 ImportJava.io.InputStreamReader;9 /**Ten * Byte stream is read into a character flow One * @authorGanhang A * - */ - Public classInputstreamreaderdemo { the Public Static voidMain (string[] args) { -File File =NewFile ("1.txt"); - InputStream in; - Try { +in =Newfileinputstream (file); -InputStreamReader isr=NewInputStreamReader (in); + intLen=-1; AStringBuffer sb=NewStringBuffer (); at Char[]b=New Char[10]; - while((Len=isr.read (b))!=-1){ -Sb.append (b,0, Len); - } - isr.close (); - in.close (); in System.out.println (SB); -}Catch(FileNotFoundException e) { to e.printstacktrace (); +}Catch(IOException e) { - e.printstacktrace (); the } * } $}
1 PackageCom.io;2 ImportJava.io.File;3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 ImportJava.io.OutputStream;7 ImportJava.io.OutputStreamWriter;8 /**9 * Character flow is byte writeTen * @authorGanhang One * A */ - Public classOutputstreamwriterdemo { - Public Static voidMain (string[] args) { theFile file=NewFile ("1.txt"); - OutputStream out; - Try { -out =NewFileOutputStream (file,true); +OutputStreamWriter osw=NewOutputStreamWriter (out); -String info= "haha haha haha"; + Osw.write (info); A osw.close (); at out.close (); -System.out.println ("Write succeeded!")); -}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); in } - } to}
The conversion of Java learning character stream and byte stream