Java read txt file and overwrite write TXT file and append write txt

Source: Internet
Author: User

Tag:res   oid    Path    null   new    ase   +=   create    document    

Create file public static void CreateFile (file filename) {try {if (!filename.exists ()) {Filename.createnewfile ()}} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} Write TXT content is not overwritten append write public static Boolean filechasewrite (String content,string filepath) {Boolean flag=false;try { FileWriter fw=new FileWriter (filepath,true); Fw.write (Content); Fw.flush (); Fw.close (); flag=true;} catch (Exception e) {//E.printstacktrace ();} return flag;} Write txt content overwrite original content public static Boolean writetxtfile (String content,string filepath) {Boolean flag=false;try {// The path to the TXT document written PrintWriter pw=new PrintWriter (filepath);//write content Pw.write (contents);p W.flush ();p w.close (); flag=true;} catch (Exception e) {e.printstacktrace ();} return flag;} Read txt content public static String readtxtfile (file file) {string sresult= ""; try {InputStreamReader reader=new InputStreamReader (new FileInputStream (file), "GBK"); BufferedReader br=new BufferedReader (reader); String S=null;while ((S=br.readline ())!=null) {sresult+=s; System.out.println (s);}}catch (Exception e) {e.printstacktrace ();} return sresult;}


Java read txt file and overwrite write TXT file and append write txt

Related Article

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: 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.