IO manipulation character streams in Java: FileReader and FileWriter

Source: Internet
Author: User
Tags readline

FileReader and FileWriter Read and write Strings

1  PackageObject.io;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 ImportJava.io.FileReader;6 ImportJava.io.FileWriter;7 Importjava.io.IOException;8 9  Public classFileWrite {Ten  One      Public Static voidMain (string[] args)throwsIOException { A         //TODO auto-generated Method Stub -FileReader reader=NewFileReader ("D:\\Program Files (x86) \\io\\fileWriter.txt");  -BufferedReader breader=NewBufferedReader (reader); theFileWriter writer=NewFileWriter ("D:\\Program Files (x86) \\io\\fileWriter2.txt"); -BufferedWriter bwriter=NewBufferedWriter (writer); -String content= ""; -         //ReadLine a row of reads +          while((Content=breader.readline ())! =NULL){ -             //\ r \ n line break +Bwriter.write (content+ "\ r \ n"); A         } at         /** - * Order of closing streams: - * When a depends on B, turn off A and then close B - * Flush is performed once the buffered stream is last closed -          */ - reader.close (); in breader.close (); - bwriter.close (); to writer.close (); +          -     } the  *}

Operation Result:

  

Practice:

1  PackageObject.io;2 3 ImportJava.io.BufferedReader;4 ImportJava.io.BufferedWriter;5 Importjava.io.FileNotFoundException;6 ImportJava.io.FileReader;7 ImportJava.io.FileWriter;8 Importjava.io.IOException;9 Ten  Public classFileWrite2 { One  A      Public Static voidMain (string[] args)throwsIOException { -         //TODO auto-generated Method Stub -FileReader reader=NewFileReader ("D:\\Program Files (x86) \\io\\fileWriter11.txt");  theBufferedReader breader=NewBufferedReader (reader); -FileWriter writer=NewFileWriter ("D:\\Program Files (x86) \\io\\fileWriter22.txt"); -BufferedWriter bwriter=NewBufferedWriter (writer); -String content= ""; +          while((Content=breader.readline ())! =NULL){ -             if(Content.contains ("{title}")){ +Content=content.replace ("{title}", "Holiday"); A             } at             if(Content.contains ("{Description}")){ -Content=content.replace ("{description}", "New Year's Day holiday three days"); -             } -Bwriter.write (content+ "\ r \ n"); -         } -         /** in * Order of closing streams: - * When a depends on B, turn off A and then close B to * Flush is performed once the buffered stream is last closed +          */ - reader.close (); the breader.close (); * bwriter.close (); $ writer.close ();Panax Notoginseng          -     } the  +}

Results:

IO manipulation character streams in Java: FileReader and FileWriter

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.