Java read txt file and write TXT file simple example _java

Source: Internet
Author: User
Tags readline

Write Java programs often encountered to read such as txt or write TXT file, but because to define a lot of variables, often remember, every time to check, hereby tidy up, simple and easy to use, convenient understood!

Package edu.thu.keyword.test;
Import Java.io.File;
Import Java.io.InputStreamReader;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.FileInputStream;

Import Java.io.FileWriter;
			public class Cin_txt {static void Main (String args[]) {try {//Prevent file creation or read failure, catch errors with catch and print, or read txt files throw/* * String pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt"; Absolute or relative paths, this is an absolute path, and the relative path file filename = new file (pathname) is shown when writing to files; To read input from the above path. TXT file InputStreamReader reader = new InputStreamReader (new FileInputStream (filename)); Create an input stream object reader BufferedReader br = new BufferedReader (reader);
			Create an object that turns the contents of the file into a language that the computer can read, String line = "";
			line = Br.readline (); while (lines!= null) {line = Br.readline ();//read one row of data}/* Write TXT file/file Writename = new File (". \\resul T\\en\\output.txt "); Relative path, and if not, create a new output. TXT file writename.createnewfile (); Create a new file bufferedwriter out = BufferedWriterWriter (Writename)); Out.write ("I'll write to the file \ r \ n"); \ r \ n is the line Out.flush (); Pressing the contents of the buffer into the file Out.close ();
		Finally remember to close the file} catch (Exception e) {e.printstacktrace (); }
	}
}

Above this java read TXT file and write TXT file Simple example is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.