Java I/O Streams and File Handling

FreeAlgebra avatar
FreeAlgebra
·
·
Download

Start Quiz

Study Flashcards

16 Questions

What is a stream in Java?

An object that represents data being delivered to or from an object

How does a stream deliver data?

One byte at a time or one character at a time

What is a code example of a stream?

FileInputStream and FileOutputStream

What are the two kinds of streams in Java?

Byte Streams and Character Streams

What do Byte Streams do?

Read and write raw bytes

What do Character Streams do?

Read and write characters (16-bit unicode)

Why is buffering used in Java?

To access a big lump of data at a time

What is expensive in terms of accessing data?

Accessing data outside of memory

What is the purpose of the LineNumberReader?

To keep track of line numbers in a character-input stream.

What is setLineNumber() used for in LineNumberReader?

To designate the line number of the first line.

What is the purpose of the BufferedReader?

To read a line from a character-input stream.

What is the purpose of the PrintWriter?

To print formatted representations of objects to a text-output stream.

What is the default behavior of PrintWriter?

Auto-flush on newline.

What can a PrintWriter wrap?

Either a Writer or an OutputStream.

What is the purpose of the File class?

To wrap a String, which may not denote something on the file system.

What is the purpose of the InputStreamReader?

To convert bytes from an InputStream to chars using the proper encoding.

Study Notes

Streams

  • A stream is an object that represents data being delivered to or from an object, one byte or character at a time.
  • There are two kinds of streams: Byte Streams and Character Streams (also known as "readers" and "writers").
  • Byte Streams read and write raw bytes, while Character Streams read and write characters (16-bit Unicode).

Readers and Writers

  • Readers and Writers are Character Streams that read and write characters.
  • Examples of Readers and Writers include FileReader, FileWriter, and PrintWriter.

Buffering

  • Buffering is used to improve performance when accessing data outside of memory (e.g., disk, network, web).
  • It is more efficient to access a large chunk of data at once rather than one byte or character at a time.

LineNumberReader

  • A LineNumberReader is a buffered character-input stream that keeps track of line numbers.
  • The setLineNumber() method does not seek to a line in the file, but rather designates the line number of the first line.

InputStreamReader and PrintWriter

  • InputStreamReader converts bytes from an InputStream to characters, using the proper encoding.
  • PrintWriter prints formatted representations of objects to a text-output stream.
  • System.out is a PrintWriter.

Using Streams on the Web

  • Streams can be used to read from a URL object, URLConnection object, and write to a URLConnection object or HttpServletResponse object.

File

  • A File object is a wrapper for a String, and does not necessarily denote something on the file system.
  • A File object can be created with any string, regardless of whether it's a valid filename or not.

Learn about Java streams, readers, writers, buffers, and files. Understand the concept of streams, their behavior, and how they are used in Java I/O operations.

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 8: Streams Infinitos
5 questions

Java 8: Streams Infinitos

EffortlessPhosphorus avatar
EffortlessPhosphorus
Java File and Stream Handling
8 questions
Use Quizgecko on...
Browser
Browser