Example: A simple Java program to show the concept of Input/Output Stream.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
// Create a file input stream
FileInputStream in = new FileInputStream(new File("input.txt"));
// Create a file output stream
FileOutputStream out = new FileOutputStream(new File("output.txt"));
// Read data from input file and write it to output file
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
// Close streams
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 Comments