Learning objectives
- Understand basic principles of File I/O
- Understand and be able to use File I/O exceptions
- Be able to write out to a text file in the desired format
- Be able to read in text from a file
What is Java IO?
- Java I/O means Input and Output.
- It helps Java programs read data (input) and write data (output).
- java has java.io.package for IO operations
- java.io.package also helps with file handling
What is stream?
- Stream is a sequence of data or like a pipe that carries data.
- Java has default 3 streams created and attached to the console
- System.out,System.in,System.err
- A stream is composed of bytes.
- Closing a stream when it's no longer needed.
Text I/O
- A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file.
- You need to create objects using appropriate Java I/O classes.
- File input -reading data from files
- File output- writing data to files
The File Class
- Provides the template for File objects that allow abstraction of the file and path names
- Wrapper class for filename and directory path
- Does not provide the ability to read from or write to files
- Github code sample FileClass
Writing to a File
- We use FileWriter to write "Hello, Java! I love coding!" into a file called myFile.txt.
- Github code sample FileClass
Java I/O Categories
- The classes in the I/O package divide input and output streams into other categories
- An I/O stream is either a.
Character Streams: For working with text data (like words and sentences).
- Byte Streams: For working with binary data (like images and audio files).
- Java I/O API provides Interfaces for reading files
- Streams: Used to read binary data
- Readers: Used to read character/text Example: FileReader (reads characters from a file).
- FileReader: Reads in characters
- BufferedReader: Buffering of data for faster reading
- Scanner: provides parsing ability Scanner (reads and processes text from a file or keyboard).
BufferedReader
- Uses a buffer to allow the reading of full lines of text at a time rather than one byte at a time
- Github code sample BufferedReader
Scanner
- Creates a Scanner object to read data from the specified file
- Scanner object has several methods
- Java.util.Scanner package
- Github code sample Scanner
Writing Data To a File
FileWriter:
BufferedWriter:
Writes data to a file more efficiently by using an internal buffer.
Reduces the number of direct writes to the file, improving performance.
Example: Writing large amounts of data.
- PrintWriter:
FileWriter
- Writes text to character files using a default buffer size
- The FileWriter is meant for writing streams of characters
- Github code sample FileWriter
BufferedWriter
- Read/write string from /to text files.
br = new BufferedReader(new FileReader(f));
bw = new BufferedWriter(new FileWriter(f));
s = br.readLine() Returns next input line without newline char(s) or null if no more input. May throw IOException.
s = br.close() Close when finished reading to unlock file.
bw.writeLine(s) Writes s to file without newline char(s). May throw IOException.
bw.newLine() Writes operating-system dependent newline (CR, LF, or CRLF).
bw.close() Call close() when finished writing, otherwise data may not be written to disk.
BufferedWriter
- Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
- Github code sample BufferedWriter
PrintWriter
- Prints formatted representations of objects to a text-output stream.
- This class implements all of the print methods found in PrintStream.
- Github code sample PrintWriter
FileOutputStream
- A file output stream is an output stream for writing data to a File or to a FileDescriptor.
- FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
- Github code sample FileOutputStream
Java NIO
- Non-Blocking I/O:
- Allows a thread to perform other tasks while waiting for I/O operations to complete.
- Example: A thread can read data from a file while simultaneously performing other computations.
- Channels and Buffers:
- Channels are like pipes that connect to I/O sources (e.g., files, sockets).
-
Buffers are containers for data, used to read from or write to channels.
- Files API:
- Provides utility methods for file operations, such as reading, writing, copying, and deleting files.
- Simplifies common file tasks compared to the traditional
java.io package.
- Github code sample NIO methods
Example: Non-Blocking I/O with Channels and Buffers