Skip to content

Text files: write and read with Scanner · ⁨文本文件:用 Scanner 写和读⁩

English

Reading and writing text files

  • A text file stores text that stays on disk after the program ends.
  • To read a file in Java we use a Scanner connected to a File.
  • To write a file we use a PrintWriter. In this lesson each program writes a file, then reads it back.
  • Bonus lesson: the AP CSA exam does not test file input/output. Learn this for real programs — you will not be examined on it.
中文

读写文本文件

  • 文本文件(text file)把文字存到磁盘上,程序结束后它还在。
  • 在 Java 里读文件,我们用一个连到 File 的 Scanner。
  • 写文件我们用 PrintWriter。在这一课里,每个程序先写一个文件,再把它读回来。
  • 附加内容: AP CSA 考试不考文件输入输出。学它是为了写真实的程序 —— 考试不会考。

Handout

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩