此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
读写文本文件
- 文本文件(text file)把文字存到磁盘上,程序结束后它还在。
- 在 Java 中读取文件时,我们使用连接了
File的Scanner。 - 写文件我们用
PrintWriter。在这一课里,每个程序先写一个文件,再把它读回来。 - 考试提示: 使用
File和Scanner读取文本文件是 AP CSA 主题 4.6。使用PrintWriter写入内容不在考试范围内;此处仅用于创建随后要读取的文件。
探索
读写一个文件
用文件分三步:打开它,读或写每一行,然后关闭它。
你需要的 import
- 文件工具放在两个地方,所以我们在顶部把两个都导入。
import java.io.*;—— 用于File、PrintWriter和IOException。import java.util.Scanner;—— 用于读取器Scanner。
English
import java.io.*;
import java.util.Scanner;
文件可能出错,所以我们写 throws IOException
- 读或写文件可能会出问题(文件可能不存在)。
- Java 要求你处理这种情况。最简单的方法是在
main中添加throws IOException。 - 这句话的意思是"如果发生文件错误,就让程序停下来"。对这些任务来说这样就够了。
用 PrintWriter 写文件
new PrintWriter("data.txt")会创建(或清空)一个叫data.txt的文件。- 用
writer.println(...)写一行,就像往屏幕上打印一样。 - 写完后一定要调用
writer.close(),这样文字才会被保存。
English
import java.io.*;
public class WriteDemo {
public static void main(String[] args) throws IOException {
PrintWriter writer = new PrintWriter("data.txt");
writer.println(10);
writer.println(20);
writer.close(); // save the file
System.out.println("wrote data.txt");
}
}
用 Scanner 读数字
new Scanner(new File("data.txt"))打开文件来读取。- 只要还有下一个整数,
in.hasNextInt()就是true。 in.nextInt()读取下一个整数。读完后调用in.close()。
English
import java.io.*;
import java.util.Scanner;
public class ReadNumbers {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("data.txt"));
int total = 0;
while (in.hasNextInt()) { // more numbers?
total = total + in.nextInt();
}
in.close();
System.out.println(total);
}
}
整行读取
- 有时一行是文字,而不是数字。那就改成读整行。
- 只要还有下一行,
in.hasNextLine()就是true。 in.nextLine()把下一整行作为一个String读进来。
import java.io.*;
import java.util.Scanner;
public class ReadLines {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("data.txt"));
while (in.hasNextLine()) {
String line = in.nextLine();
System.out.println("read: " + line);
}
in.close();
}
}
hasNext()(后面没有Int或Line)检查下一个单词,next()读取一个单词。- 套路始终是:先用
hasNext...方法检查,再用对应的方法读取。
在一个程序里先写后读
- 在每个任务里,你的程序先写一个文件,然后把同一个文件读回来。
- 这样你可以一遍又一遍地运行它,得到相同的结果。
English
import java.io.*;
import java.util.Scanner;
public class RoundTrip {
public static void main(String[] args) throws IOException {
PrintWriter writer = new PrintWriter("data.txt");
writer.println(3);
writer.println(4);
writer.close();
Scanner in = new Scanner(new File("data.txt"));
int total = 0;
while (in.hasNextInt()) {
total = total + in.nextInt();
}
in.close();
System.out.println(total); // 7
}
}
常见错误
nextInt()会留下换行符,导致紧接的nextLine()读到空行。- 读之前检查
hasNext(),避免读过末尾。
现在轮到你
- 每个任务都预填好了一个类。按提示要求,先写文件,再把它读回来。
- 记住
main上的throws IOException,以及close()同时包含写入者和Scanner。 - 按运行来编译并运行,然后按检查答案。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
首先将数字 10、20、5、7 写入文件 data.txt(每行一个),并在末尾添加一个 PrintWriter。然后使用 new Scanner(new File("data.txt")) 打开该文件,并用 hasNextInt/nextInt 进行求和。仅打印总和。预期输出:42。
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
首先将内容 apple、banana、cherry 写入文件 words.txt,并在末尾添加一个 PrintWriter。然后使用 Scanner 打开该文件,并用 hasNextLine/nextLine 统计行数。仅打印计数结果。预期输出:3。
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
启动器向 nums.txt 写入 4、17 和 9。使用 Scanner(hasNextInt / nextInt)读回并仅打印 最大值。预期输出:17。
点击“运行”查看此处输出。