1. 从文件中读取信息
//从文件中读取数据信息并打印输出
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner; //三个包不能少
public class Test {
public static void main(String args[]) throws IOException //这个throws不能少
{
Scanner in = new Scanner(Paths.get("d:\\1.txt"));
String data = in.nextLine();
String data2 = in.nextLine();
System.out.println(data + '\n' + data2);
}
}
2. 写入数据信息到文件中
//向文件中写入数据信息
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner; //三个包不能少
public class Test {
public static void main(String args[]) throws IOException //这个throws不能少
{
PrintWriter out = new PrintWriter("d:\\2.txt");
out.println("hahahaha"); //写入数据信息到指定文件
out.flush(); //刷新文件
}
}