Java Read from a File, Write to a File

Опубликовано: 13 Октябрь 2024
на канале: RioProfessor Liu
75
1

Rio Hondo College
CIT 136 Java Programming
To read input from a disk file, the Scanner class relies on another class, File, which describe disk files and directories.

Example: File inputFile = new File(“input.txt”);

Then use the File object to construct a Scanner object:

Scanner in = new Scanner (inputFile);

To write output to a file, you construct a PrintWriter object with the desired file name.

Example: PrintWriter out = new PrintWriter(“output.txt”);

When you are done processing a file, be sure to close the Scanner or PrintWriter:

In.close();

Out.close();
Professor Liu