The following code is mainly implemented in Java read files and write files, practice the flow operation.
Important: 1. When reading the file, be sure to add the encoding format, otherwise Chinese garbled
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.util.ArrayList;
Import java.util.List;
public class Readerorwrite {
public static void Main (string[] args) {
Readerorwrite rw =new readerorwrite ();
Rw.mywrite ("E:\\parse\\output.txt", "lalalalalaal--this is the content, \ r \ n");
Rw.myread ("E:\\parse\\output.txt");
}
public void Mywrite (String filepath,string content) {
try {
"E:\\parse\\output.txt"
File Writename = new file (FilePath); Relative path, if not, to create a new output. TXT file
Writename.createnewfile (); Create a new file
BufferedWriter out = new BufferedWriter (new FileWriter (Writename));
Out.write (content); \ r \ n is the wrapped file content
Out.flush (); Press the buffer contents into the file
Out.close (); Finally close the file
} catch (IOException e) {
E.printstacktrace ();
}
}
Public list<string> Myread (String pathname) {
list<string> linelist = new arraylist<string> ();
try{
String 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 = new file (pathname); The input to read the above path. TXT file
InputStreamReader reader = new InputStreamReader (new FileInputStream (filename), "UTF-8"); Create an input stream object reader
Missing Utf-8 will be garbled in Chinese
BufferedReader br = new BufferedReader (reader); Create an object that turns the contents of a file into a language that the computer can read
String line = "";
line = Br.readline ();
while (line! = null) {
Linelist.add (Br.readline ());
line = Br.readline (); Read one row of data at a time
System.out.println (line); Test what is read
}
}catch (Exception e) {
E.printstacktrace ();
}
return linelist;
}
}
Java Write and read Demo