The difference between the FileInputStream class and the FileReader class:
The form and parameters of the constructors for two classes are the same, the arguments are file objects, or the string that represents the path, what is the difference between them?
? Readers and writers work is only on line based character data, so plain text files.
For anything else, you must use Streams.
? JDK5 API:
FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.
FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a fileinputstream.
? FileInputStream: read by byte stream; FileReader: Convert file to character stream read in;
? The InputStream provides a byte stream reading, not a text read, which is fundamentally different from the reader class. Read in reader is a char array or a string, using InputStream to read a byte array.
? The reader class and its subclasses provide a stream of characters that reads char (16-bit, Unicode encoding), InputStream and its subclasses provide byte stream reads in bytes (8 bits), so the FileReader class reads the file as a stream of characters, FileInputStream reads the file by byte stream; InputStreamReader can convert read as stream into character stream, which is the bridge between reader and stream.
? Originally Java was not supporting the processing of text files, in order to compensate for this shortcoming introduced the reader and writer two classes.
? The FileInputStream class takes binary input/output, I/O speed and efficiency, but its read () method reads a byte (binary data), which is detrimental to people's reading.
? And the FileReader class compensates for this flaw, can be in text format input/output, very convenient, such as can use while ((ch = filereader.read ())!=-1) loop to read the file ; You can use BufferedReader's ReadLine () method to read text one line at a line.
? When we read and write text files, it is very convenient to use reader, such as FileReader, InputStreamReader and BufferedReader. One of the most important classes is InputStreamReader, which is a bridge that converts bytes into characters. You can specify the encoding in the constructor, and if you do not specify it, the default encoding for the underlying operating system, such as GBK, is used.
? FileReader and InputStreamReader involve encoding conversion (specifying encoding or using OS default encoding), which may appear garbled on different platforms! The FileInputStream is processed in binary mode, and there is no garbled phenomenon.
3) Self-understanding:
? If you are working with plain text files, it is recommended to use FileReader because it is more convenient and more suitable for reading, but pay attention to coding problems!
? In other cases (processing of non-plain text files), FileInputStream is the only option; FileInputStream is a lot of use when it comes to socket communication, such as the way the file stream is stream to the server!
3. FileReader class
1) FileReader class Introduction:
Subclasses of the InputStreamReader class, all methods (read (), etc.) are inherited from the parent class InputStreamReader;
2) Differences from the InputStreamReader class:
? Self-Understanding:
The main difference between this class and its parent class InputStreamReader is the constructor function, which is mainly the constructor! As seen from the InputStreamReader constructor, the parameters are InputStream and encoded, and you can see that you must use the InputStreamReader class when you want to specify the encoding method While the parameters of the FileReader constructor are the same as the FileInputStream, which is a file object or a string representing path, it can be seen that the FileReader I think the role of FileReader is in this small division of labor.
3) General usage:
FileReader FR = new FileReader ("Ming.txt");
char[] buffer = new char[1024];
int ch = 0;
while ((ch = fr.read ())!=-1)
{
System.out.print ((char) ch);
}
4. InputStreamReader class
? Input/output in text format, you can specify the encoding format;
? Main methods:
GetEncoding (), read ();
? General usage:
InputStreamReader ISR = new InputStreamReader (New FileInputStream ("Ming.txt"));
while ((ch = isr.read ())!=-1)
{
System.out.print ((char) ch);
}
5. BufferedReader class
? JDK5 API:
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, AR Rays, and lines.
? BufferedReader is extended by the reader class, providing a common buffer way text read, and provides a very useful readline, read the branch text is suitable, BufferedReader is for reader, not directly against the file, It is not read only for files.
? General usage:
BufferedReader br = new BufferedReader (new InputStreamReader (New FileInputStream ("Ming.txt"));
String data = null;
while (data = Br.readline ())!=null)
{
SYSTEM.OUT.PRINTLN (data);
}
6. Summarize the above and draw a good standard usage:
1) file File = new file ("Hello.txt");
FileInputStream in=new fileinputstream (file);
2) file File = new file ("Hello.txt");
FileInputStream in=new fileinputstream (file);
InputStreamReader inreader=new InputStreamReader (in);
BufferedReader bufreader=new BufferedReader (Inreader);
3) File File = new file ("Hello.txt");
FileReader filereader=new filereader (file);
BufferedReader bufreader=new BufferedReader (FileReader);
Java----The difference between the FileInputStream class and the FileReader class (RPM)