In a java programming, like a Byte stream Character stream is also used for input-output for 16 bit. But Byte streams are used to perform input and output of 8-bit.
Most common Character streams classes are, FileReader and FileWriter.
FileReader- FileReader uses for reads two bytes at a time.
FileWriter– FileWriter uses for writes two bytes at a time.
Example: Write a program, to copy the content of file A into file B using Byte stream class.
import java.io.*;
public class FileReaderExample {
public static void main(String arg[]) throws IOException {
FileReader I = null;
FileWriter O = null;
try {
I = new FileReader("A.txt");
O = new FileWriter("B.txt");
int c;
while ((c = I.read()) != -1) {
O.write(c);
}
} finally {
if (I != null) {
I.close();
}
if (O != null) {
O.close();
}
}
}
}
Note: if there is only file A in which data is written and no file B then, file B will be create automatically.
Suppose we have a File “A”
| Hello students I am aditya. |
Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.
File “B”
| Hello students I am aditya. |