Byte stream is divided into FileInputStream and FileOutputStream
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 /**9 * Read the file byte streamTen * @authorGanhang One * A */ - Public classFileinputstreamdemo { - Public Static voidMain (string[] args) { theFile file=NewFile ("1.txt"); - Try { -InputStream is=Newfileinputstream (file); - byte[] b=New byte[10]; + intLen=-1; -StringBuilder sb=NewStringBuilder ();//Memory Read Data + while((Len=is.read (b))!=-1){ ASb.append (NewString (b,0, Len)); at } - is.close (); - System.out.println (SB); -}Catch(FileNotFoundException e) { - e.printstacktrace (); -}Catch(IOException e) { in e.printstacktrace (); - } to + } -}
1 PackageCom.io;2 3 ImportJava.io.File;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileOutputStream;6 Importjava.io.IOException;7 ImportJava.io.OutputStream;8 /**9 * Writing of file byte streamTen * @authorGanhang One * A */ - Public classFileoutputstreamdemo { - Public Static voidMain (string[] args) { theFile File =NewFile ("1.txt"); - if(!file.exists ()) { - Try { -File.createnewfile ();//create file if not +}Catch(IOException e) { - e.printstacktrace (); + } A}Else { at Try { -OutputStream fos =NewFileOutputStream (file,true);//The end of the file is added, not overwritten - byte[] info = "Hello,world". GetBytes (); - Fos.write (info); - fos.close (); -System.out.println ("Write succeeded! "); in}Catch(FileNotFoundException e) { - e.printstacktrace (); to}Catch(IOException e) { + e.printstacktrace (); - } the } * } $}
Java Learning IO byte stream