Java file input and output PrintWriter and Scanner summary

Tips: Use the Scanner class to read text data from the file, and use the PrintWriter class to write data to the text file.

The File object encapsulates the attributes of a file or path, but it neither includes methods to create files, nor methods to read/write data from/to files (called data input and output, or I/O for short). In order to complete the I/O operation, an object needs to be created using the appropriate JavaI/O class.
The following describes how to use the Scanner and PMntWriter classes to read (write) character strings and numerical information from (to) text files

Use PrintWriter to write data to the file

The java.io.PrintWriter class can be used to create a file and write data to a text file.

step:

  1. Create a PrintWriter object for a text file as follows:
    PrintWriter output = new PrintWriter(filename);
  2. Call the print, println and printf methods on the PrinterWriter object to write data to the file.
    The following summarizes the commonly used methods in PrintWriter:
    Insert picture description here
    Usage: output.print() can input data to the file represented by output.

Here is an analogy to deepen the understanding:
we use the System.out.print, System.out.println and System.out.printf methods to output text to the console.

  • System.out is the standard Java object of the console.
  • The same output is an object of the file we created, and then use print, println and printf to write text to the file.

note:

  1. If the file already exists, call PrintWriter, then the current content of the file will be discarded without confirming with the user.
  2. Calling the constructor of PrintWriter may throw some kind of I/O exception. Java mandates writing code to handle such exceptions. Just declare throws Exception in the method header declaration.
  3. The file must be closed using the close() method. If this method is not called, the data cannot be saved in the file correctly.

Use try-with-resources to automatically close resources

From the above, we can know that we must use the close() method to close the file. But often forget to close the file. JDK7 provides the following new try -with -resources syntax to automatically close files.


try (declare and create resources) { Use resources to process files; }


Example:
Insert picture description here
Description:

  • A resource is declared and created after the keyword try. Note that the resources are in parentheses. The resource must be a subtype of AutoCloseable, such as PrinterWHter, which has a close() method.
  • The declaration and creation of resources must be in the same line of statements, and multiple resource declarations and creations can be carried out in parentheses.
  • The statement in the block immediately following the resource declaration uses the resource. After the block ends, the close() method of the resource is automatically called to close the resource.
  • Using try -with -resourse can not only avoid errors, but also simplify the code.

Use Scanner to read data


Quote : The Java.util.Scanner class is used to read strings and basic types of values ​​from the console.

  • Scanner can divide the input into marks separated by blank characters.
  • In order to be able to read from the keyboard, you need to create a Scanner for System.in,

As shown below: Scanner input = new Scanner(System.in);

Similarly, in order to read from the file, you need to create a Scanner for the file, as shown below:


Scanner input = new Scanner(new File (filename));


The following figure summarizes the commonly used methods of the Scanner class:
Insert picture description here
Description:

  1. new Scanner(String) creates a Scanner for the given string.
    To create a Scanner to read data from a file, you must use the construction method new File (filename) to
    create an instance of File using the java.io.File class, and then use new Scanner (File) to create a Scanner for the file.
  2. Calling the constructor new Scanner(File) may throw an I/O exception. Therefore, throws Exception must also be declared in the method header.
  3. There is no need to close the input file, but doing so is a good way to release the resources occupied by the file. You can also use try-with-resources syntax to write programs.

To deepen the understanding of Scanner, please follow this link:
Scanner working mechanism-mark reading method, in-depth understanding of the difference between next() and nextline()

Guess you like

Origin blog.csdn.net/qq_45768060/article/details/106492537