If the files are stored in byte format in the end then why do we have FileReader and FileWriter for text files and FileInputStream and FileOutputStream for byte files?
How can we know before handling a file that a file is a text document file(without checking the secondary name, ) or a byte format file
The fact of the matter is that all files are in byte format. However in the case of text files, it is simply the case that those bytes represent characters.
In fact, one can view any file in text form. It's just that it might not make any sense
There is no easy way to check format because 'format' can mean many, many different things.
Now, as to why there are different types of stream classes.
Input can come from many different sources. Reading from a file is for instance quite different then reading from the network.
As for your example of FileReaders and FileInputStreams and all that:
You do not NEED to use different streams. It's just that readers/writers are more convient for text. You COULD use Input/Output streams.
I was expecting this code to display text file
It compiles well, and when executing displays some weird characters.
How do I make it display the content of the textfile shakun.doc ditto?
import java.io.*;
public class FileOper
{
public static void main(String args[])throws IOException{
FileReader fr=new FileReader("c:\\Bluej\\examples\\shakun.doc");
BufferedReader p=new BufferedReader(fr);
String s;
while((s=p.readLine())!=null){
System.out.println(s);
}
p.close();
}
}