InputStream and OutputStream are the input and output streams in a byte stream.
Public classFileStream { Public Static voidMain (string[] args) {FileStream fs=NewFileStream (); //String content =//Fs.readfilewithinputstream ("/home/rding/file/file1.txt"); //System.out.println (content);Fs.writefilewithoutputstream ("/home/rding/file/file3.txt","Hello File3"); //fs.copyfile ("/home/rding/file/file3.txt",//"/home/rding/file/file4.txt"); } Public voidcopyFile (String srcpath, String descpath) {InputStream instream=NULL; OutputStream OutStream=NULL; Try{instream=NewFileInputStream (Srcpath); OutStream=NewFileOutputStream (Descpath); intSize =instream.available (); byte[] Bytearr =New byte[size]; Instream.read (Bytearr); Outstream.write (Bytearr); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } Public voidWritefilewithoutputstream (String filePath, string content) {OutputStream OutStream=NULL; byte[] Bytearr =content.getbytes (); Try {
Note that after the OutStream is first established, it will not be updated and must be closed and re-established OutStream=NewFileOutputStream (FilePath); //OutStream = new FileOutputStream (new File (FilePath)); If the passed parameter is true, then the original file continues to be written, instead of overwriting the source file. //OutStream = new FileOutputStream (FilePath, true); //OutStream = new FileOutputStream (new File (FilePath), true);Outstream.write (Bytearr); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(OutStream! =NULL) { Try{outstream.close (); } Catch(IOException e) {e.printstacktrace (); } } } } Publicstring Readfilewithinputstream (String filePath) {InputStream instream=NULL; String result=""; Try{instream=NewFileInputStream (FilePath); //instream = new FileInputStream (new File (FilePath)); intSize =instream.available (); byte[] Bytearr =New byte[size]; Instream.read (Bytearr); Result=NewString (Bytearr); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } finally { if(Instream! =NULL) { Try{instream.close (); } Catch(IOException e) {e.printstacktrace (); } } } returnresult; }}
Java file stream byte stream