Java Input/Output
Java Input/Output (I/O) is an important concept in Java as it is used for reading data from and writing data to different sources. The Java I/O API provides several input/output classes that help in performing various I/O operations.
Syntax
Here's a basic syntax for input/output in Java:
// Reading from a file
FileInputStream fileInputStream = new FileInputStream("input.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] buffer = new byte[1024];
while ((bufferedInputStream.read(buffer)) > -1) {
// Perform operations on buffer
}
bufferedInputStream.close();
fileInputStream.close();
// Writing to a file
FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
String data = "Hello, world!";
bufferedOutputStream.write(data.getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();
fileOutputStream.close();
Example
In this example, we will read data from a file and write it to another file using Java I/O:
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream("input.txt");
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, bytesRead);
}
bufferedInputStream.close();
bufferedOutputStream.close();
System.out.println("File copied successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
The output of the above example will be a new file called output.txt
that contains the same data as the original input file.
Explanation
In the code above, we use two input/output classes: FileInputStream
and FileOutputStream
. The FileInputStream
class reads data from the specified file, while the FileOutputStream
class writes data to the specified file.
To improve performance, we wrap the FileInputStream
and FileOutputStream
with BufferedInputStream
and BufferedOutputStream
respectively. These classes help in reducing the number of disk accesses by transferring data in larger chunks.
After reading data from the input file, we write it to the output file using a byte array. The bytesRead
variable stores the number of bytes read from the input file on each cycle of the while
loop and is used to write the correct amount of data to the output file.
Use
Java I/O is used to read and write data from and to different sources, such as files, streams, and sockets. It is used extensively in file processing, network programming, and database programming.
Important Points
Here are some important points to keep in mind when working with Java I/O:
- Always wrap input/output streams with buffered streams to improve performance.
- Always close input/output streams after use to free up resources.
- Java I/O provides several classes for reading and writing different types of data, such as
DataInputStream
andDataOutputStream
.
Summary
Java Input/Output is an important part of Java programming as it enables reading and writing data from different sources. The Java I/O API provides several input/output classes that help in performing different types of I/O operations. When using Java I/O, it's important to wrap input/output streams with buffered streams, close streams after use, and choose the right input/output class for the job.