I am working on I/O classes in Java. I understand that there are two important type of streams: byte stream and character stream. But... I have tried to read and write text file with byte stream and it worked. Here is the code:
File klasor = new File("C:\\Java");
if(!klasor.exists()) klasor.mkdirs();
File kaynakDosya = new File("C:\\Java\\kaynak.txt");
if(!kaynakDosya.exists()) kaynakDosya.createNewFile();
File hedefDosya = new File("C:\\Java\\hedef.txt");
if(!hedefDosya.exists()) hedefDosya.createNewFile();
FileInputStream kaynak = new FileInputStream(kaynakDosya);
FileOutputStream hedef = new FileOutputStream(hedefDosya);
int c;
while((c = kaynak.read()) != -1) {
hedef.write(c);
}
if(kaynak != null) {
kaynak.close();
}
if(hedef != null) {
hedef.close();
}
And then I did the same with character stream:
File klasor = new File("C:\\Java");
if(!klasor.exists()) klasor.mkdirs();
File kaynakDosya = new File("C:\\Java\\kaynak.txt");
if(!kaynakDosya.exists()) kaynakDosya.createNewFile();
File hedefDosya = new File("C:\\Java\\hedef.txt");
if(!hedefDosya.exists()) hedefDosya.createNewFile();
FileReader kaynak = new FileReader(kaynakDosya);
FileWriter hedef = new FileWriter(hedefDosya);
int c;
while((c = kaynak.read()) != -1) {
hedef.write(c);
}
if(kaynak != null) {
kaynak.close();
}
if(hedef != null) {
hedef.close();
}
These two produced the same result. So, I want to know, why shouldn't I use byte stream here but character stream? (I have read some articles as well as related questions here on stackoverflow and they say so) I know that character stream will read it character by character, but what advantage does this give me? Or what problems could occur if I read characters using byte stream? I hope my question is clear. I would appreciate real-case examples.
java.io.FileInputStream
javadoc states :
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
java.io.FileOutputStream
javadoc states something similar enough :
FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.
One of main differences between FileInputStream/FileOutputStream
and FileReader/FileWriter
is that the first provides methods to manipulate bytes while the latter provides methods to manipulate characters.
In your example, as you copy a file content into another file, manipulating char
or byte
doesn't make a big difference.
In your case, a FileInputStream
or a BufferedInputStream
seems even more appropriate.
But if you use a stream to read/write characters from/into String
instances,
using FileReader/FileWriter
eases really the things and make things clearer.
Besides, you could also wrap FileReader/FileWriter
into a BufferedReader/BufferedWriter
and benefit from efficient reading/writting of characters, arrays, and lines.
BufferedWriter writer = new BufferedWriter(new FileWriter("myfile"));
writer.append(oneString);
writer.append(oneStringBuffer);
writer.newLine();
BufferedReader reader = new BufferedReader(new FileReader("myfile"));
String currentLine = reader.readLine();