Writing to Java files

Source: Internet
Author: User

A write file is similar to a read file, which can be written in bytes and can be written in a character unit.

The corresponding read operation FileOutputStream is written in bytes:

1FileOutputStream fileoutputstream=NULL;2String astring= "Hello";3         Try {4fileoutputstream=NewFileOutputStream (afile);5 //fileoutputstream=new FileOutputStream ("E:/a.txt");6 //fileoutputstream=new FileOutputStream (afile,true);7 Fileoutputstream.write (Astring.getbytes ()); 8}Catch(FileNotFoundException e) {9             //TODO auto-generated Catch blockTen e.printstacktrace (); One}Catch(IOException e) { A             //TODO auto-generated Catch block - e.printstacktrace (); -}finally { the             if(fileoutputstream!=NULL) -             { -                 Try { -Fileoutputstream.flush ();//The write method does not output data immediately to the specified destination, is staged in memory, and flush () indicates that the data in the buffer is forced to be sent out + fileoutputstream.close (); -}Catch(IOException e) { +                 //TODO auto-generated Catch block A e.printstacktrace (); at                 } -             } -}

Here is just a simple replacement, if you want to implement the additional words only need to write FileOutputStream (afile,true); write the second argument as true.

The Filewriter,bufferedwriter is written in characters and can be written directly to the string

1FileWriter filewriter=NULL;2         Try {3Filewriter=NewFileWriter (afile);4 //FileWriter filewriter=new FileWriter ("");5 //FileWriter filewriter=new FileWriter (afile,true);//file Continuation, append6 Filewriter.write (astring); 7}Catch(IOException e) {8             //TODO auto-generated Catch block9 e.printstacktrace ();Ten}finally { One             if(filewriter!=NULL) A             { -                 Try { - Filewriter.flush (); the filewriter.close (); -}Catch(IOException e) { -                     //TODO auto-generated Catch block - e.printstacktrace (); +                 } -             } +}

Writing to Java files

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.