Podcast
Questions and Answers
What happens if a Scanner is closed before data is input?
What happens if a Scanner is closed before data is input?
IllegalStateException occurs
Can Class Scanner reposition to the beginning of a file?
Can Class Scanner reposition to the beginning of a file?
False (B)
Why are records in a sequential-access file usually not updated in place?
Why are records in a sequential-access file usually not updated in place?
- To prevent the risk of destroying other data (correct)
- To save memory space
- To improve file security
- Because sequential files do not support updates
What is the purpose of storing data in files?
What is the purpose of storing data in files?
Java views each file as a sequential stream of bytes.
Java views each file as a sequential stream of bytes.
File streams can be used to input and output data as ______ or characters.
File streams can be used to input and output data as ______ or characters.
What type of files are created using byte-based streams?
What type of files are created using byte-based streams?
Match the file stream with its corresponding usage:
Match the file stream with its corresponding usage:
Flashcards are hidden until you start studying
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.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.