File IO

Cards (10)

  • Input data may be read from the keyboard and file
  • Output may be displayed on the output screen
  • Output may be written in a file
  • In Java, there are classes that supports input and/or output through input and output files
  • Some classes for file input/output (FileIO) are defined in the java.io package.
  • Programs that use the classes must have the import java.io.* statement.
    • BufferedReader
    • InputStreamReader
    • PrintWriter
    • OutputStream
  • Reading input data through the keyboard may be allowed by using the BufferedReader class.
  • Sometimes, you want to add the program output to the end of an existing text file.
    This process is called appending to a file.
  • To be able to append, create an object of the PrintWriter class and connect it to the text file as shown below. 

    Notice the use of the “true” boolean expression.
  • The Scanner class that is in the java.util.Scanner; package may also be used for reading/writing data from/to files.
    Hence, import java.util.Scanner; must be in the program.