Overview of Basic Input and Output in Java
<aside>
<img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents
</aside>
<aside>
💡
- Basic Input and Output in Java
</aside>
<aside>
💡
- Types of I/O Streams
</aside>
<aside>
💡
- Differences Between Byte Streams and Character Streams
</aside>
Basic Input and Output in Java
-
Java I/O
- I/O in Java (Input and Output) deals with reading data from sources (input) and writing data to destinations (output). Java provides a rich set of classes under the
java.io
package for performing input and output operations. These operations include file handling, console input/output, object serialization, and more.
-
Key Concepts of Java I/O
- Stream:
- A stream is a sequence of data.
- Input Stream: Reads data from a source.
- Output Stream: Writes data to a destination.
- Examples: Reading/writing files, console input/output.
- Byte Streams vs. Character Streams:
- Byte Streams:
- Handles raw binary data (e.g., images, videos).
- Classes:
InputStream
, OutputStream
.
- Character Streams:
- Handles textual data (e.g., reading/writing text files).
- Classes:
Reader
, Writer
.
- Serialization:
- A mechanism of converting an object into a byte stream for storage or transmission.
- Classes:
ObjectInputStream
, ObjectOutputStream
.
-
Types of Streams
- Byte Streams
- Designed to handle binary data.
- Classes:
- InputStream (Abstract class): Reads byte data.
- Examples:
FileInputStream
, BufferedInputStream
.
- OutputStream (Abstract class): Writes byte data.
- Examples:
FileOutputStream
, BufferedOutputStream
.
- Character Streams
- Designed to handle text data (characters).
- Classes:
- Reader (Abstract class): Reads character data.
- Examples:
FileReader
, BufferedReader
.
- Writer (Abstract class): Writes character data.
- Examples:
FileWriter
, BufferedWriter
.
-
Best Practices for Java I/O
- When working with Java I/O, consider the following best practices:
- Use Try-with-Resources:Â Always use the try-with-resources statement when working with streams to ensure proper resource management. This automatically closes the streams when they are no longer needed, preventing resource leaks.
- Use Buffered I/O:Â Whenever possible, use buffered I/O classes to minimize the number of system calls and improve performance.
- Handle Exceptions Gracefully:Â Handle I/O exceptions gracefully by implementing error handling mechanisms such as logging or error propagation.
- Use NIO for Performance:Â For high-performance I/O operations, consider using Java's NIO (New I/O) package, which provides non-blocking I/O features and enhanced performance.
-
File Handling in Java
-
File handling allows reading from and writing to files. Common classes used are:
-
File Class
- Represents file and directory pathnames.
-
Methods:
createNewFile()
: Creates a new file.
mkdir()
: Creates a new directory.
exists()
: Checks if the file exists.
delete()
: Deletes the file.
-
Example:
import java.io.File;
public class FileExample {
public static void main(String[] args) {
File file = new File("example.txt");
try {
if (file.createNewFile()) {
System.out.println("File created: " + file.getName());
} else {
System.out.println("File already exists.");
}
} catch (Exception e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}