File, fileinputstream, filereader, inputstreamreader, bufferedreader...
References:
L Chapter 12 of core Java
L detailed description of how to operate text files using Java
Http://java.ccidnet.com/art/3737/20041108/523627_1.html
L what is filereader class? What is the difference with fileinputstream ???
Http://book.hackbase.com/ask2/ask107572.htm
Organize and comprehend by yourself:
Introduction:
C language only requires a file *. Unlike C, Java has a series of stream types with over 60 types. The designer of the class library claims :"
There are enough reasons for users to choose a wide range of stream types: this can reduce program errors ." For example, in C, many people think that
Writing an output stream to a file in read-only mode is a common error. (In fact, this is not common .)
We believe that in C ++, the main "tool" for stream interface designers to avoid program errors is a cautious attitude, especially in Java.
Here. The high complexity of the stream library forces programmers to be cautious.
1. File class
1) Introduction to the file class (core Java 638 page)
The file class encapsulates the function of operating the file system on the user's machine. For example, you can use the File class to get the last modification time of the file,
Or delete or rename the file. In other words, the stream class focuses on the file content, while the file class focuses on the storage of files on disks.
.
The main methods of the file class include getname (), getcanonicalfile (), lastmodified (), isderector (), isfile (), getpath ()
And so on;
2) differences between the file class and the fileinputstream class:
The stream class focuses on the file content, while the file class focuses on the storage of files on disks.
File is not a file stream and can only represent a file or directory path.
Tip: (core Java page 1)
If you are processing the file or directory name, you should use the file object instead of the string. For example, the equals method of the file class knows some
The file system is case sensitive and the "/" character at the end of the directory does not matter.
Understanding:
The fileinputstream class or the filereader class has multiple constructors. The typical two are: one uses the file object
The other uses the string object indicating the path as the parameter. You have always thought that you can directly use the string to specify the path.
I have never understood why many people construct a file object first. Now I understand that "if you process the file or directory name, you should
This uses a file object instead of a string ."!
2. fileinputstream class
1) fileinputstream class introduction:
Stream processing in bytes (non-Unicode. The byte sequence is binary data. It has nothing to do with encoding, and there is no garbled problem.
The main methods of the fileinputstream class are:
Read (), read (byte [] B), read (byte [], int off, int Len), available ();
2) differences between the fileinputstream class and the filereader class:
The constructor form and parameters of the two classes are the same. The parameters are file objects or string representing paths. What are the differences between them?
What about it?
L readers and writers work only on line based character data, so plain text files.
For anything else, you must use streams.
L 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.
L fileinputstream: reads data in byte streams; filereader: converts a file to a bytes stream for reading;
L inputstream provides byte stream reading instead of text reading, which is the fundamental difference with the reader class. Read from Reader
It is a char array or string, and the byte array is read using inputstream.
L reader class and its subclass provide the reading char (16-bit, Unicode encoding) for the bytes stream, and inputstream and its subclass provide the bytes
Stream reading byte (8 bits), so the filereader class reads files in bytes stream mode, and fileinputstream reads files in byte stream mode.
Reading files; inputstreamreader can convert a stream, for example, to a bytes stream, which serves as a bridge between reader and stream.
L at first, Java does not support text file processing. To make up for this defect, two classes, reader and writer, were introduced.
L fileinputstream class uses binary input/output. I/O is fast and efficient, but its Read () method reads
It is a byte (binary data), which is not conducive to reading.
L The filereader class makes up for this defect and can be input/output in text format, which is very convenient; for example, it can be used.
While (CH = filereader. Read ())! =-1) read the file cyclically. You can use the Readline () method 1 of bufferedreader.
Read text of a row.
L when we read and write text files, It is very convenient to use reader, such as filereader,
Inputstreamreader and bufferedreader. The most important class is inputstreamreader, which is the bridge between byte conversion and character conversion.
Beam. You can specify the encoding method in the constructor. If you do not specify the encoding method, the default encoding method of the underlying operating system will be used, such as GBK.
.
L filereader and inputstreamreader involve encoding conversion (specify the encoding method or use OS default encoding ).
Garbled characters can appear on different platforms! Fileinputstream is processed in binary format without garbled characters.
3) Understanding:
L if you are dealing with plain text files, we recommend that you use filereader because it is more convenient and suitable for reading.
!
L in other cases (processing non-plain text files), fileinputstream is the only choice; fileinputstream is used for socket communication.
It is used a lot. For example, the file stream is transmitted to the server in stream mode!
3. filereader class
1) Introduction to the filereader class:
Subclass of the inputstreamreader class. All methods (such as read () are inherited from the parent class inputstreamreader;
2) differences with the inputstreamreader class:
L understanding:
The main difference between this class and its parent class inputstreamreader lies in the constructor. The main difference lies in the constructor! Slave
The inputstreamreader constructor shows that the parameters are inputstream and the encoding method. We can see that when you want to specify the encoding method,
The inputstreamreader class must be used. The parameters of the filereader constructor are the same as those of fileinputstream, which is a file object or
It indicates the string of path. It can be seen that when you want to read a file based on the file object or string, use filereader; I
I think the role of the filereader subclass lies 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
L input/output in text format. The encoding format can be specified;
L main methods:
Getencoding (), read ();
L General Usage:
Inputstreamreader ISR = new inputstreamreader (New fileinputstream ("ming.txt "));
While (CH = ISR. Read ())! =-1)
{
System. Out. Print (char) CH );
}
5. bufferedreader class
L jdk5 API:
Read text from a character-input stream, buffering characters so as to provide for the efficient
Reading of characters, arrays, and lines.
L bufferedreader is extended by the reader class and provides a general buffer mode for text reading, and provides a very practical Readline,
It is suitable for reading branch text. bufferedreader is intended for reader, not for files, but for reading files only.
L 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 content to get a better standard usage:
1) file = new file ("hello.txt ");
Fileinputstream in = new fileinputstream (File );
2) file = new file ("hello.txt ");
Fileinputstream in = new fileinputstream (File );
Inputstreamreader inreader = new inputstreamreader (in );
Bufferedreader bufreader = new bufferedreader (inreader );
3) file = new file ("hello.txt ");
Filereader = new filereader (File );
Bufferedreader bufreader = new bufferedreader (filereader );
7. Differences in writing:
1)
File file = new file ("hello.txt ");
Fileinputstream in = new fileinputstream (File );
Inputstreamreader inreader = new inputstreamreader (in );
Bufferedreader bufreader = new bufferedreader (inreader );
2)
Fileinputstream in = NULL;
File file = new file ("hello.txt ");
In = new fileinputstream (File );
Bufferedreader bufreader = new bufferedreader (New inputstreamreader (in ));
3)
File file = new file ("hello.txt ");
Bufferedreader bufreader = new bufferedreader (New inputstreamreader (New fileinputstream (File )));
The minor differences between the two methods are as follows:
A) In the second method, put the "fileinputstream in = NULL;" definition separately at the beginning, indicating that the in object variable should be used below; (bufferedreader used)
B) the second method does not define the object variable of inputstreamreader. It is directly added to the constructor of bufferedreader,
The main difference between this method and the first method is that the inputstreamreader object is only used once!
This is better for an application that only needs to use this inputstreamreader object once. It does not need to define the inputstreamreader object variable and receives references to this object returned by new, the following program does not need to define the inputstreamreader object variable. In this case, the second method is better than the first one.
C) In the third method, the typical layer-3 nested delegation relationship clearly shows the reader's delegation mode (chapter 12 of corejava describes the delegation relationship in a diagram ), neither fileinputstream nor inputstreamreader defines variables. The new object is only used once.
D) The difference between the three methods is whether the fileinputstream and inputstreamreader objects are only used once, whether their object variables need to be defined, and individual coding habits.
E) but pay attention to Exception Handling. fileinputstream (File) will throw notfilefoundexception. If the surround method is used
(Try & catch), the second method should be used, so that system. Out. println can be used to prompt that the file is not found;
Of course, you can use throws exception after the function name, and then use the third method, but it seems that this is suitable for a user interface, throwing an exception to the client for processing.
References:
L Chapter 12 of core Java
L detailed description of how to operate text files using Java
Http://java.ccidnet.com/art/3737/20041108/523627_1.html
L what is filereader class? What is the difference with fileinputstream ???
Http://book.hackbase.com/ask2/ask107572.htm
Organize and comprehend by yourself:
Introduction:
C language only requires a file *. Unlike C, Java has a series of stream types with over 60 types. The designer of the class library claims :"
There are enough reasons for users to choose a wide range of stream types: this can reduce program errors ." For example, in C, many people think that
Writing an output stream to a file in read-only mode is a common error. (In fact, this is not common .)
We believe that in C ++, the main "tool" for stream interface designers to avoid program errors is a cautious attitude, especially in Java.
Here. The high complexity of the stream library forces programmers to be cautious.
1. File class
1) Introduction to the file class (core Java 638 page)
The file class encapsulates the function of operating the file system on the user's machine. For example, you can use the File class to get the last modification time of the file,
Or delete or rename the file. In other words, the stream class focuses on the file content, while the file class focuses on the storage of files on disks.
.
The main methods of the file class include getname (), getcanonicalfile (), lastmodified (), isderector (), isfile (), getpath ()
And so on;
2) differences between the file class and the fileinputstream class:
The stream class focuses on the file content, while the file class focuses on the storage of files on disks.
File is not a file stream and can only represent a file or directory path.
Tip: (core Java page 1)
If you are processing the file or directory name, you should use the file object instead of the string. For example, the equals method of the file class knows some
The file system is case sensitive and the "/" character at the end of the directory does not matter.
Understanding:
The fileinputstream class or the filereader class has multiple constructors. The typical two are: one uses the file object
The other uses the string object indicating the path as the parameter. You have always thought that you can directly use the string to specify the path.
I have never understood why many people construct a file object first. Now I understand that "if you process the file or directory name, you should
This uses a file object instead of a string ."!
2. fileinputstream class
1) fileinputstream class introduction:
Stream processing in bytes (non-Unicode. The byte sequence is binary data. It has nothing to do with encoding, and there is no garbled problem.
The main methods of the fileinputstream class are:
Read (), read (byte [] B), read (byte [], int off, int Len), available ();
2) differences between the fileinputstream class and the filereader class:
The constructor form and parameters of the two classes are the same. The parameters are file objects or string representing paths. What are the differences between them?
What about it?
L readers and writers work only on line based character data, so plain text files.
For anything else, you must use streams.
L 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.
L fileinputstream: reads data in byte streams; filereader: converts a file to a bytes stream for reading;
L inputstream provides byte stream reading instead of text reading, which is the fundamental difference with the reader class. Read from Reader
It is a char array or string, and the byte array is read using inputstream.
L reader class and its subclass provide the reading char (16-bit, Unicode encoding) for the bytes stream, and inputstream and its subclass provide the bytes
Stream reading byte (8 bits), so the filereader class reads files in bytes stream mode, and fileinputstream reads files in byte stream mode.
Reading files; inputstreamreader can convert a stream, for example, to a bytes stream, which serves as a bridge between reader and stream.
L at first, Java does not support text file processing. To make up for this defect, two classes, reader and writer, were introduced.
L fileinputstream class uses binary input/output. I/O is fast and efficient, but its Read () method reads
It is a byte (binary data), which is not conducive to reading.
L The filereader class makes up for this defect and can be input/output in text format, which is very convenient; for example, it can be used.
While (CH = filereader. Read ())! =-1) read the file cyclically. You can use the Readline () method 1 of bufferedreader.
Read text of a row.
L when we read and write text files, It is very convenient to use reader, such as filereader,
Inputstreamreader and bufferedreader. The most important class is inputstreamreader, which is the bridge between byte conversion and character conversion.
Beam. You can specify the encoding method in the constructor. If you do not specify the encoding method, the default encoding method of the underlying operating system will be used, such as GBK.
.
L filereader and inputstreamreader involve encoding conversion (specify the encoding method or use OS default encoding ).
Garbled characters can appear on different platforms! Fileinputstream is processed in binary format without garbled characters.
3) Understanding:
L if you are dealing with plain text files, we recommend that you use filereader because it is more convenient and suitable for reading.
!
L in other cases (processing non-plain text files), fileinputstream is the only choice; fileinputstream is used for socket communication.
It is used a lot. For example, the file stream is transmitted to the server in stream mode!
3. filereader class
1) Introduction to the filereader class:
Subclass of the inputstreamreader class. All methods (such as read () are inherited from the parent class inputstreamreader;
2) differences with the inputstreamreader class:
L understanding:
The main difference between this class and its parent class inputstreamreader lies in the constructor. The main difference lies in the constructor! Slave
The inputstreamreader constructor shows that the parameters are inputstream and the encoding method. We can see that when you want to specify the encoding method,
The inputstreamreader class must be used. The parameters of the filereader constructor are the same as those of fileinputstream, which is a file object or
It indicates the string of path. It can be seen that when you want to read a file based on the file object or string, use filereader; I
I think the role of the filereader subclass lies 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
L input/output in text format. The encoding format can be specified;
L main methods:
Getencoding (), read ();
L General Usage:
Inputstreamreader ISR = new inputstreamreader (New fileinputstream ("ming.txt "));
While (CH = ISR. Read ())! =-1)
{
System. Out. Print (char) CH );
}
5. bufferedreader class
L jdk5 API:
Read text from a character-input stream, buffering characters so as to provide for the efficient
Reading of characters, arrays, and lines.
L bufferedreader is extended by the reader class and provides a general buffer mode for text reading, and provides a very practical Readline,
It is suitable for reading branch text. bufferedreader is intended for reader, not for files, but for reading files only.
L 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 content to get a better standard usage:
1) file = new file ("hello.txt ");
Fileinputstream in = new fileinputstream (File );
2) file = new file ("hello.txt ");
Fileinputstream in = new fileinputstream (File );
Inputstreamreader inreader = new inputstreamreader (in );
Bufferedreader bufreader = new bufferedreader (inreader );
3) file = new file ("hello.txt ");
Filereader = new filereader (File );
Bufferedreader bufreader = new bufferedreader (filereader );
7. Differences in writing:
1)
File file = new file ("hello.txt ");
Fileinputstream in = new fileinputstream (File );
Inputstreamreader inreader = new inputstreamreader (in );
Bufferedreader bufreader = new bufferedreader (inreader );
2)
Fileinputstream in = NULL;
File file = new file ("hello.txt ");
In = new fileinputstream (File );
Bufferedreader bufreader = new bufferedreader (New inputstreamreader (in ));
3)
File file = new file ("hello.txt ");
Bufferedreader bufreader = new bufferedreader (New inputstreamreader (New fileinputstream (File )));
The minor differences between the two methods are as follows:
A) In the second method, put the "fileinputstream in = NULL;" definition separately at the beginning, indicating that the in object variable should be used below; (bufferedreader used)
B) the second method does not define the object variable of inputstreamreader. It is directly added to the constructor of bufferedreader,
The main difference between this method and the first method is that the inputstreamreader object is only used once!
This is better for an application that only needs to use this inputstreamreader object once. It does not need to define the inputstreamreader object variable and receives references to this object returned by new, the following program does not need to define the inputstreamreader object variable. In this case, the second method is better than the first one.
C) In the third method, the typical layer-3 nested delegation relationship clearly shows the reader's delegation mode (chapter 12 of corejava describes the delegation relationship in a diagram ), neither fileinputstream nor inputstreamreader defines variables. The new object is only used once.
D) The difference between the three methods is whether the fileinputstream and inputstreamreader objects are only used once, whether their object variables need to be defined, and individual coding habits.
E) but pay attention to Exception Handling. fileinputstream (File) will throw notfilefoundexception. If the surround method is used
(Try & catch), the second method should be used, so that system. Out. println can be used to prompt that the file is not found;
Of course, you can use throws exception after the function name, and then use the third method, but it seems that this is suitable for a user interface, throwing an exception to the client for processing.