Java IO Read and write operations

Source: Internet
Author: User

Read of file: public static void Duqu (String path) throws ioexception{file file=new file (path); FileInputStream fis=new fileinputstream (file); InputStreamReader isr=new inputstreamreader (FIS, "gb2312"); BufferedReader br=new BufferedReader (ISR); StringBuffer buf=new StringBuffer (); String Str=null;while ((Str=br.readline ())!=null) {buf.append (str); Buf.append ("\ r \ n");} System.out.println (Buf.tostring ()); Br.close ();}          Write to file: public static void Write (string path, string content) {string s = new String ();                    string S1 = new String ();                            try {file F = new File (path);              if (f.exists ()) {System.out.println ("file exists");                  } else {System.out.println ("file does not exist, creating ..."); if (F.createnewfile ()) {System.out.println ("file created successfully!                  "); } else {System.out.println ("File creation failed!                  "); }} BuffeRedreader input = new BufferedReader (new FileReader (f));              while ((s = input.readline ()) = null) {S1 + = s + "/n";              } input.close ();              S1 + = content;              BufferedWriter output = new BufferedWriter (new FileWriter (f));              System.out.println ("File contents:" + S1);              Output.write (S1);          Output.close ();          } catch (Exception e) {e.printstacktrace (); }      }

  

Java IO Read and write operations

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