1 PackageCom.io;2 3 ImportJava.io.*;4 /**5 * Read the file character stream6 * @authorGanhang7 *8 */9 Public classFilereaderdemo {Ten Public Static voidRead () { OneFile File =NewFile ("1.txt"); A Try { -FileReader fr=Newfilereader (file); -StringBuffer sb=NewStringBuffer (); the Char[] cbuf=New Char[10]; - intLen=-1; - while((Len=fr.read (CBUF))!=-1){ -Sb.append (cbuf,0, Len); + } - fr.close (); + System.out.println (SB); A}Catch(FileNotFoundException e) { at e.printstacktrace (); -}Catch(IOException e) { - e.printstacktrace (); - } - - in } - Public Static voidMain (string[] args) { to read (); + } -}
1 PackageCom.io;2 3 ImportJava.io.*;4 /**5 * Write the file character stream6 * @authorGanhang7 *8 */9 Public classFilewriterdemo {Ten Public Static voidwrite () { OneFile File =NewFile ("1.txt"); A Try { -FileWriter FR =NewFileWriter (file,true); -String str= "haha haha"; the fr.write (str); - fr.close (); -System.out.println ("Write succeeded! "); -}Catch(IOException e) { + e.printstacktrace (); - } + } A Public Static voidMain (string[] args) { at write (); - } -}
Java Learning IO character stream