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.