quiz image

File IO

LuxuryAbundance avatar
LuxuryAbundance
·
·
Download

Start Quiz

Study Flashcards

40 Questions

Which interface is implemented by classes whose objects represent a file or directory?

Path

Which class has static methods that get a Path object that represents a file or directory location?

Paths

What is the purpose of the Files class?

To manipulate files: create, copy and delete files

What is the separator used in CSV files?

Commas (,)

How are values within a row separated in a CSV file?

By commas (,)

What is the format of a CSV file?

Name, Age, City

What is the purpose of the directoryStream interface?

To loop through directory contents

What type of file uses semicolons (;) as separators?

Text file

What is the difference between CSV and Text files?

CSV uses commas, Text uses semicolons

What is the format of a Text file?

Name; Age; City

What is a key difference between a variable and a file in terms of data storage?

Variables store temporary data, while files store permanent data.

What is the purpose of the end-of-file marker in Java?

To mark the end of a file.

What type of files are created by byte-based streams in Java?

Binary files

What package is used to import file and directory information in Java?

java.nio

What is the purpose of the Scanner class in Java?

To read input data.

What is the difference between character-based streams and byte-based streams in Java?

Character-based streams create text files, while byte-based streams create binary files.

What is the purpose of serialization in Java?

To read and write objects.

How many bytes are used to represent each character in character-based streams in Java?

2 bytes

What is the main difference between text files and binary files?

Text files store data in characters, while binary files store data in bytes.

What is the purpose of the Formatter class in Java?

To format data for output.

What is included in a serialized object?

The object's data and its type information

What is the purpose of the ObjectOutputStream class?

To write objects to a stream

What is necessary to use serialization with files?

Initialize both ObjectInputStream and ObjectOutputStream objects

What is the main purpose of serialization?

To read and write objects to a file

What is the relationship between ObjectInputStream and ObjectOutput?

They are two separate classes that implement ObjectInput and ObjectOutput interfaces

What happens when an object is deserialized?

The object is recreated in memory

What is the purpose of the ObjectInputStream class?

To read objects from a stream

Why are ObjectOutputStream and ObjectInputStream necessary?

To read and write entire objects

What is serialization used for?

To read and write entire objects

What is the result of deserializing an object?

The object is recreated in memory

What is the purpose of the Scanner class in Java?

To read information from a text file

What is the consequence of not providing a path when using a Formatter?

The file will be created in the default directory

What is the purpose of the Formatter class in Java?

To write formatted strings to a file

What is the consequence of attempting to write to a file without permission?

A SecurityException will be thrown

What is the purpose of System.exit?

To terminate an application

What is the significance of an argument of 0 when using System.exit?

It indicates successful program termination

What is the purpose of object serialization in Java?

To read or write an entire object to or from a file

What exception is thrown when a file does not exist and cannot be created?

FileNotFoundException

What exception is thrown when the data being read by a Scanner method is in the wrong format?

NoSuchElementException

What exception is thrown when a Formatter is closed and an attempt is made to output to it?

FormatterClosedException

Study Notes

File I/O

  • A computer file is a permanent storage of data, unlike variables and arrays which are temporary.
  • There are two basic types of computer files: Text files and Binary files.

Files and Streams

  • Each file is viewed as a stream of bytes by Java.
  • There are two types of streams: Byte-based streams (input and output data in byte format, creates binary files) and Character-based streams (input and output data in character format, creates text files).

NIO Classes and Interfaces

  • The java.nio package has classes and interfaces to get file and directory information: path, directoryStream, paths, files.
  • The Path interface is implemented by classes whose objects represent a file or directory.
  • The directoryStream interface is implemented by classes to loop through directory contents.
  • The Paths class has static methods that get a Path object that represents a file or directory location.
  • The Files class has static methods for manipulating files: create, copy, and delete files.

CSV File vs Text File

  • CSV files use commas as separators, while text files use separators such as commas, colons, or semicolons.
  • CSV files have each subsequent line representing a data row, with values within a row separated by commas.

Reading and Writing Files

  • The Scanner class is used to read text files.
  • The Formatter class is used to write formatted strings to a specified stream.

Exceptions

  • SecurityException: occurs if the user does not have permission to write data to the file.
  • FileNotFoundException: occurs if the file does not exist and a new file cannot be created.
  • NoSuchElementException: if the data being read by a Scanner method is in the wrong format or if there is no more data to input.
  • FormatterClosedException: if the Formatter is closed when attempting to output.

Object Serialization

  • Object Serialization is the process of converting an object into a byte stream, which can be written to a file.
  • The ObjectOutputStream and ObjectInputStream classes (in the java.io package) enable entire objects to be read from or written to a stream.
  • Serialization is used to read an entire object from or write an entire object to a file.

This quiz covers the basics of object-oriented programming in Java, including file I/O operations. Designed for students in Computer Engineering Technology and Computer Science programs.

Make Your Own Quizzes and Flashcards

Convert your notes into interactive study material.

Get started for free

More Quizzes Like This

Java OOP Quiz
10 questions

Java OOP Quiz

FastestValley avatar
FastestValley
Java OOP Chapter 4 Quiz
10 questions

Java OOP Chapter 4 Quiz

TerrificSerendipity avatar
TerrificSerendipity
Use Quizgecko on...
Browser
Browser