Java text file read/write

Source: Internet
Author: User
1. Reading and Writing from one file to another

Public static void main (string [] ARGs )...{

Try ...{
File rfile = new file ("E:/test/text1.txt ");
File wfile = new file ("E:/test/text2.txt ");
Bufferedreader BR = new bufferedreader (New filereader (rfile ));
Bufferedwriter BW = new bufferedwriter (New filewriter (wfile, true);/* If this parameter is set to false, the original content of text2.txt is overwritten */

String tempstr = Br. Readline ();

While (tempstr! = NULL)
...{
// BW. Write (tempstr );
Bw. append (tempstr );
Bw. newline ();
Bw. Flush ();
Tempstr = Br. Readline ();

}
Bw. Close (); // close this first
BR. Close ();
} Catch (exception e )...{
System. Out. println (E. getmessage ());
}
}

 

 

2. Get the characters entered from the keyboard and write them to the file

 

Try ...{
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
System. Out. Print ("enter a line :");
String strin = stdin. Readline ();
System. Out. println (strin );
Bufferedwriter BW = new bufferedwriter (New filewriter (new file ("E:/test/text2.txt ")));
Bw. Write (strin );
Bw. Flush ();
Stdin. Close ();
Bw. Close ();
} Catch (exception e )...{
System. Err. println (E. getmessage ());
}

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.