废话不多说直接上代码
import java.util.Arrays;
import java.io.*;
public class FileOperate {
public static void main(String args[]) {
System.out.println(System.getProperty("java.class.path"));
Run.writeFile();
Run.readFile();
}
}
class Run {
public static void readFile() {
String pathname = "D:\\代码\\java_xy\\Hello\\src\\input.txt";
try {
FileReader reader = new FileReader(pathname);
BufferedReader br = new BufferedReader(reader);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeFile() {
BufferedWriter out = null;
try {
File writeName = new File("D:\\代码\\java_xy\\Hello\\src\\output.txt");
writeName.createNewFile();
FileWriter writer = new FileWriter(writeName);
out = new BufferedWriter(writer);
out.write("懵婷\r\n");
out.write("扫地僧\r\n");
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}