1 Packageedu.thu.keyword.test;2 3 ImportJava.io.File;4 ImportJava.io.InputStreamReader;5 ImportJava.io.BufferedReader;6 ImportJava.io.BufferedWriter;7 ImportJava.io.FileInputStream;8 ImportJava.io.FileWriter;9 Ten Public classCin_txt { One Static voidMain (String args[]) { A Try{//prevents file creation or read failures, catches errors with catch and prints, or can throw - - /*read in TXT file*/ theString pathname = "D:\\twitter\\13_9_6\\dataset\\en\\input.txt";//absolute or relative path, this is an absolute path, and a relative path is shown when writing to a file -File filename =NewFile (pathname);//the input to read the above path. TXT file -InputStreamReader reader =NewInputStreamReader ( - NewFileInputStream (filename));//Create an input stream object reader +BufferedReader br =NewBufferedReader (reader);//create an object that turns the contents of a file into a language that the computer can read -String line = ""; +line =br.readline (); A while(Line! =NULL) { atline = Br.readline ();//read one row of data at a time - } - - /*Write TXT file*/ -File Writename =NewFile (". \\result\\en\\output.txt");//relative path, if not, to create a new output. TXT file -Writename.createnewfile ();//Create a new file inBufferedWriter out =NewBufferedWriter (NewFileWriter (Writename)); -Out.write ("I'll write the file \ r \ n");//\ r \ n is the line break toOut.flush ();//press the buffer contents into the file +Out.close ();//Finally, remember to close the file - the}Catch(Exception e) { * e.printstacktrace (); $ }Panax Notoginseng } -}
Transferred from: http://blog.csdn.net/monkeyduck/article/details/11525771
Go: Java reads txt file and writes TXT file