Java I/o help


 
Thread Tools Search this Thread
Top Forums Programming Java I/o help
# 1  
Old 10-01-2007
Java I/o help

what the different between:

File a = new File("xanadu.txt");

in = new FileInputStream("xanadu.txt");

inputStream = new BufferedReader(new FileReader("xanadu.txt"));
# 2  
Old 10-01-2007
Actually, the javadoc already tells you what the differences are. From java.io.FileInputStream:

Quote:
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
Source: FileInputStream (Java Platform SE 6)

BufferedReader implements a line-buffered layer on top of FileReader to allow convenient extraction of lines, and usually buffered I/O is more efficient. But of course, this is only relevant to text files because binary files have no notion of "lines".

Also, the fact that FileInputStream is byte-oriented and FileReader is character-oriented will be obvious when it comes to reading multibyte files, that uses an encoding where a character consists of 2 or more bytes. FileReader will decode properly because that is character-oriented.

Last edited by cbkihong; 10-01-2007 at 09:26 AM.. Reason: Added comment on multibyte
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question