Java IO Learning Basics of Read and write text files

Source: Internet
Author: User
Java IO operations are based on the flow of operations, in order to improve the reading and writing efficiency generally need to buffer.

The simple example program is as follows:

/**
* read out the contents of 1.txt, write to 2.txt
*
*/

Import java.io.*;

public class readwritefile{
public static void Main (string[] args) {
try{

File Read = new file ("C:\\1.txt");
File Write = new file ("C:\\2.txt");

BufferedReader br = new BufferedReader (
New FileReader (read));
BufferedWriter bw = new BufferedWriter (
New FileWriter (write));
String temp = null;
temp = Br.readline ();
while (temp!= null) {
Write a file
Bw.write (temp + "\ r \ n"); Only Windows systems available
Continue reading files
temp = Br.readline ();
}

Bw.close ();
Br.close ();

}catch (FileNotFoundException e) {//File not found
System.out.println (e);
}catch (IOException e) {
System.out.println (e);
}
}
}




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.