数据写入
public void write() throws FileNotFoundException {
PrintWriter printWriter = new PrintWriter("src/test/data.txt");
printWriter.println(123);
printWriter.print(1233);
printWriter.close();
}
注意:需要有close()才能成功
运行结果:
数据读取
public void read() throws FileNotFoundException {
File file = new File("src/test/data.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
}
运行结果: