Java file stream byte stream

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.