Java File and Stream Handling

UseableNephrite4314 avatar
UseableNephrite4314
·
·
Download

Start Quiz

Study Flashcards

8 Questions

What happens if a Scanner is closed before data is input?

IllegalStateException occurs

Can Class Scanner reposition to the beginning of a file?

False

Why are records in a sequential-access file usually not updated in place?

To prevent the risk of destroying other data

What is the purpose of storing data in files?

Long-term retention of data

Java views each file as a sequential stream of bytes.

True

File streams can be used to input and output data as ______ or characters.

bytes

What type of files are created using byte-based streams?

binary files

Match the file stream with its corresponding usage:

FileInputStream = byte-based input from a file FileOutputStream = byte-based output to a file FileReader = character-based input from a file FileWriter = character-based output to a file

Study Notes

Introduction to Files and Streams

  • Data stored in variables and arrays is temporary and lost when a program terminates or a local variable goes out of scope.
  • Computers use files for long-term retention of data, which exists beyond the duration of program execution.
  • Files are stored on secondary storage devices such as hard disks, optical disks, flash drives, and magnetic tapes.

Files and Streams

  • Java views each file as a sequential stream of bytes.
  • A file stream can be used to input and output data as bytes or characters.
  • Streams that input and output bytes are known as byte-based streams, representing data in its binary format.
  • Streams that input and output characters are known as character-based streams, representing data as a sequence of characters.
  • Files created using byte-based streams are referred to as binary files, while files created using character-based streams are referred to as text files.
  • A Java program opens a file by creating an object and associating a stream of bytes or characters with it.
  • Java creates three stream objects when a program begins executing: System.in, System.out, and System.err.

Class File

  • Class File provides information about files and directories.
  • Class File provides four constructors to specify the name of a file or directory to associate with the File object.
  • The constructor with a String argument specifies the name of a file or directory, which can contain path information.
  • The constructor with two String arguments specifies an absolute or relative path and the file or directory to associate with the File object.
  • The constructor with File and String arguments uses an existing File object that specifies the parent directory of the file or directory specified by the String argument.
  • The fourth constructor uses a URI object to locate the file.

Sequential-Access Text Files

  • Sequential-access files store records in order by the record-key field.
  • Text files are human-readable files.
  • Java imposes no structure on a file, and notions such as records do not exist as part of the Java language.
  • You must structure files to meet the requirements of your applications.

Creating a Sequential-Access Text File

  • Formatter outputs formatted Strings to the specified stream.
  • The constructor with one String argument receives the name of the file, including its path.
  • If a path is not specified, the JVM assumes that the file is in the directory from which the program was executed.
  • If the file does not exist, it will be created. If an existing file is opened, its contents are truncated.
  • A SecurityException occurs if the user does not have permission to write data to the file.
  • A FileNotFoundException occurs if the file does not exist and a new file cannot be created.

Reading Data from a Sequential-Access Text File

  • Scanner method hasNext determines whether the end-of-file key combination has been entered.
  • A NoSuchElementException occurs if the data being read by a Scanner method is in the wrong format or if there is no more data to input.
  • Formatter method format works like System.out.printf.
  • A FormatterClosedException occurs if the Formatter is closed when you attempt to output.
  • Formatter method close closes the file.

This quiz covers the concepts of file and stream handling, and object serialization in Java, as discussed in Java How to Program, 9/e.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Master Java 8 Streams
10 questions
Java I/O Streams Quiz
10 questions
Use Quizgecko on...
Browser
Browser