Podcast
Questions and Answers
What are the two main types of streams described?
What are the two main types of streams described?
What does the term 'streams' refer to in programming?
What does the term 'streams' refer to in programming?
Which abstract classes are provided for byte streams?
Which abstract classes are provided for byte streams?
Which of the following is NOT a source that a stream can be connected to?
Which of the following is NOT a source that a stream can be connected to?
Signup and view all the answers
What type of data do character streams manipulate?
What type of data do character streams manipulate?
Signup and view all the answers
What is the primary focus when using streams in I/O operations?
What is the primary focus when using streams in I/O operations?
Signup and view all the answers
Which stream type manipulates data as bytes?
Which stream type manipulates data as bytes?
Signup and view all the answers
What does a stream abstract away from the user?
What does a stream abstract away from the user?
Signup and view all the answers
Which of the following is true about data stored in streams?
Which of the following is true about data stored in streams?
Signup and view all the answers
Which class in Java allows for both input and output operations to arbitrary data streams?
Which class in Java allows for both input and output operations to arbitrary data streams?
Signup and view all the answers
Which method would you use to read a specified number of bytes from an input stream to an array starting from a defined offset?
Which method would you use to read a specified number of bytes from an input stream to an array starting from a defined offset?
Signup and view all the answers
What is the primary function of the flush() method in output streams?
What is the primary function of the flush() method in output streams?
Signup and view all the answers
Which class is specifically designed to improve I/O performance by using a buffer?
Which class is specifically designed to improve I/O performance by using a buffer?
Signup and view all the answers
What does the method exists() in the File class return?
What does the method exists() in the File class return?
Signup and view all the answers
When writing to a text file, which class provides methods to output formatted text in lines?
When writing to a text file, which class provides methods to output formatted text in lines?
Signup and view all the answers
Which method would you use to write a single integer byte to an output stream?
Which method would you use to write a single integer byte to an output stream?
Signup and view all the answers
What is the main purpose of the BufferedInputStream class?
What is the main purpose of the BufferedInputStream class?
Signup and view all the answers
Which method from the File class would rename an existing file?
Which method from the File class would rename an existing file?
Signup and view all the answers
How does the readLines method read data from a file?
How does the readLines method read data from a file?
Signup and view all the answers
Which class would you use to read primitive data types such as int or float from a file?
Which class would you use to read primitive data types such as int or float from a file?
Signup and view all the answers
Signup and view all the answers
Flashcards
Data Streams
Data Streams
A sequence of data that is read from a source or written to a destination. Sources can be files, memory, keyboards, etc., while destinations can be files, memory, or screens.
Byte Streams
Byte Streams
Manipulate data in bytes. Two abstract classes: InputStream and OutputStream.
Character Streams
Character Streams
Manipulate data as Unicode text streams.
What is a stream connected to?
What is a stream connected to?
Signup and view all the flashcards
Why use streams?
Why use streams?
Signup and view all the flashcards
InputStream Class
InputStream Class
Signup and view all the flashcards
OutputStream Class
OutputStream Class
Signup and view all the flashcards
Data Storage
Data Storage
Signup and view all the flashcards
File I/O
File I/O
Signup and view all the flashcards
Abstraction
Abstraction
Signup and view all the flashcards
InputStream
InputStream
Signup and view all the flashcards
OutputStream
OutputStream
Signup and view all the flashcards
BufferedReader
BufferedReader
Signup and view all the flashcards
BufferedWriter
BufferedWriter
Signup and view all the flashcards
FileInputStream
FileInputStream
Signup and view all the flashcards
FileOutputStream
FileOutputStream
Signup and view all the flashcards
File
File
Signup and view all the flashcards
FileReader
FileReader
Signup and view all the flashcards
FileWriter
FileWriter
Signup and view all the flashcards
PrintWriter
PrintWriter
Signup and view all the flashcards
Study Notes
Object-Oriented Programming: Streams and Files
- Data Streams: Data is stored as a sequence of bytes. Higher-level structures like characters or objects can be considered sequences of bytes.
- Streams: A sequence of data read from or written to a destination.
- Source: Memory, keyboard, file.
- Destination: Memory, screen, file.
- Java programs use objects of different stream types to send and receive data.
- Streams can be connected to various sources (floppy, hard disk, network, memory). The implementation abstracts away specific connections, focusing only on performing input/output (I/O) operations.
- Types of Streams:
- Byte streams: Manipulate data in bytes.
- InputStream: Abstract base class for reading byte streams.
- OutputStream: Abstract base class for writing byte streams.
- Character streams: Manipulate data as Unicode text.
- Reader: Abstract base class for reading character streams.
- Writer: Abstract base class for writing character streams.
- Byte streams: Manipulate data in bytes.
Stream Hierarchies
- InputStream Hierarchy: Shows subclasses of InputStream, like FileInputStream, PipedInputStream, DataInputStream, etc.
- OutputStream Hierarchy: Shows subclasses of OutputStream, like FileOutputStream, PipedOutputStream, DataOutputStream, etc.
- Reader Hierarchy: Shows subclasses of Reader, like BufferedReader, FileReader, InputStreamReader, etc.
- Writer Hierarchy: Shows subclasses of Writer, like BufferedWriter, FileWriter, OutputStreamWriter, etc.
Methods of InputStream
- int read(): Reads the next byte of data from the input stream.
- int read(byte[] b): Reads b.length bytes from the input stream to the byte array b.
- int read(byte[] b, int offset, int length): Reads length bytes from the input stream to the byte array b starting at offset.
- void close(): Closes the input stream.
Methods of OutputStream
- int write(int c): Writes the single byte c to the output stream.
- int write(byte[] b): Writes b.length bytes from the byte array b to the output stream.
- int write(byte[] b, int offset, int length): Writes length bytes from the byte array b, starting from offset, to the output stream.
- void close(): Closes the output stream.
- void flush(): Flushes the data from the buffer to the output stream.
Methods of Reader
- int read(): Reads the next character from the input stream.
- int read(char[] cbuf): Reads cbuf.length characters from the input stream to the char array cbuf.
- int read(char[] cbuf, int off, int len): Reads len characters from the input stream to the char array cbuf, starting at offset off.
- void close(): Closes the input stream.
Methods of Writer
- int write(int c): Writes the single character c to the output stream.
- int write(char[] cbuf): Writes cbuf.length characters from the char array cbuf to the output stream.
- int write(char[] cbuf, int off, int len): Writes len characters from the char array cbuf, starting at offset off, to the stream.
- void close(): Closes the output stream.
- void flush(): Flushes the data stream from the buffer to the output stream.
Important Stream Types
- InputStream/OutputStream: Base classes with few features.
- FileInputStream/FileOutputStream: Specifically for connecting to files.
- BufferedInputStream/BufferedOutputStream: Improve I/O performance by adding buffers.
- BufferedReader/BufferedWriter: Convert bytes to Unicode characters and strings.
Input/Output Stream Object
- To read/write data, a stream object is needed.
- The I/O stream object is attached to a data source or destination.
- Example:
BufferedReader in = new BufferedReader(new FileReader(filename));
Use of Buffered Streams
- Buffering improves I/O performance by reading/writing data in blocks.
- Reduces the number of accesses to I/O devices.
- Data is written to a buffer, and when it fills, it's transferred to the destination.
- The flush() method forces data to be written to destination.
- Data is read from the buffer when it's not empty.
Standard I/O Streams
- Java.lang package includes System.out and System.err (PrintStream).
- These can be used directly to print to the standard output and error streams.
- System.in is an InputStream object, used with InputStreamReader and BufferedReader.
The File Class
- In java.io package, provides basic file and directory operations.
- Files are not streams; they represent a file structure.
- Create/open files.
- Query directory information.
Create a File Object
File myFile = new File("data.txt");
orFile myFile = new File("myDocs", "data.txt");
- Directories are treated the same as files. Example:
File myDir = new File("myDocs");
File's Methods
- File/directory name: getName(), getPath(), getAbsolutePath(), getParent(), renameTo(File newName)
- File/directory status: exists(), canWrite(), canRead(), isFile(), isDirectory()
- Status: lastModified(), length(), delete()
- Directory: mkdir(), list()
Manipulate Text Files
- Read: FileReader (for individual characters), BufferedReader(for reading lines).
- Write: FileWriter (for individual characters), PrintWriter (for writing lines).
Read from a Text File
- Uses a BufferedReader, typically linked to a FileReader, to read lines from a file until the end of the file.
- The
readLine()
method is used to read lines from the file, and the returned string is printed to the console.
Write to a Text File
- Uses a PrintWriter, typically linked to FileWriter, to write lines into the file.
out.write()
is used to write formatted data or strings into the file.
Manipulate Binary Files
- Read: FileInputStream, DataInputStream, ObjectInputStream
- Write: FileOutputStream, DataOutputStream, ObjectOutputStream
DataInputStream/DataOutputStream
- DataInputStream: Reads primitive data types. -Methods include readBoolean, readByte, readChar, readShort, readInt, readLong, readFloat, readDouble.
- DataOutputStream: Writes primitive data types.
- Methods include writeBoolean, writeByte, writeChar, writeShort, writeInt, writeLong, writeFloat, writeDouble
Write Primitive Data
- Code example showing how to write primitive data to a binary file using FileOutputStream and DataOutputStream.
Read Primitive Data
- Code example showing how to read primitive data from a binary file using FileInputStream and DataInputStream.
File of Objects
- Objects can be stored in files by using serialization.
- Data classes must implement the Serializable interface to be serialized.
Write Object
- The code to write an object to a file, using serialization with ObjectOutputStream and FileOutputStream.
Read Objects
- The code to read an object from a file using deserialization with ObjectInputStream and FileInputStream.
RandomAccessFile Class
- Implements DataInput and DataOutput interfaces.
- Enables random access to data within a file.
- Allows reading and writing data at specific byte offsets.
Write with RandomAccessFile
- Code example for writing to a file using RandomAccessFile.
Read with RandomAccessFile
- Code example for reading from a file using RandomAccessFile.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.