Several Classes for reading files in Java

Source: Internet
Author: User

At first, Java does not support text file processing. To make up for this defect, two classes, reader and writer, were introduced.

Java. Io has two abstract classes: inputstream and reader.

Inputstream is a superclass that represents all classes of the byte input stream.

Reader is an abstract class used to read the livestream.

Inputstream provides byte stream reading instead of text reading, which is the fundamental difference with the reader class.

Reader reads the char array or string, and inputstream reads the byte array.

The fundamental differences between the two superclasses are clarified. Let's take a look at the usage of the sub-classes below them. Here we only provide the most common descriptions.

I. Introduction:



(1)


Inputstream


|
_ Fileinputstream






FileInputStream


Obtain the input bytes from a file in the file system.

Constructor Summary

FileInputStream

(File
 file)


Create a file by opening a connection to the actual fileFileInputStream
.
File
Objectfile
.

FileInputStream

(FileDescriptor
 fdObj)


By using a file descriptorfdObj
Create
FileInputStream
The file descriptor represents an existing connection to an actual file in the file system.

FileInputStream

(String
 name)


Create a file by opening a connection to the actual fileFileInputStream
.
name
.



(2) bufferedreader



Reader



| -- Bufferedreader


| ___ Inputstreamreader


|__ Filereader






Bufferedreader
:
Read text from the character input stream, buffer each character, so as to achieve efficient reading of characters, arrays and rows.



Constructor Summary

BufferedReader

(Reader
 in)


Create a buffer character input stream that uses the default size input buffer.

BufferedReader

(Reader
 in,
int sz)


Creates a buffer character input stream that uses the specified size input buffer.

Bufferedreader (Java platform SE 6)


The biggest feature of bufferedreader is the buffer settings.
Generally, each read request made by reader will lead to a corresponding Read Request for the underlying character or byte stream. If no buffer is available, read () or Readline () is called each time ()
It will lead to reading bytes from the file and converting them into characters before returning, which is extremely inefficient.

You can use bufferedreader to specify the buffer size or use the default size. In most cases, the default value is large enough.

Therefore, we recommend that you use bufferedreader to wrap all its Read ()
Reader with high overhead (such as filereader and inputstreamreader ).

For example:

Bufferedreader in = new bufferedreader (New filereader ("foo. In "));

 

(3)


Inputstreamreader (Java platform SE 6)




Inputstreamreader is a bridge between byte streams: it uses the specified charset

Read bytes and decode them into characters. Its character set can be specified by name or explicitly specified, or it can accept the default Character Set of the platform.



Constructor Summary

InputStreamReader

(InputStream
 in)


Create an inputstreamreader that uses the default character set.

InputStreamReader

(InputStream
 in, Charset
 cs)


Creates an inputstreamreader that uses the given character set.

InputStreamReader

(InputStream
 in, CharsetDecoder
 dec)


Creates an inputstreamreader that uses the specified character set decoder.

InputStreamReader

(InputStream
 in, String
 charsetName)


Creates an inputstreamreader that uses the specified character set.



Call one read () in inputstreamreader each time ()
The method will read one or more bytes from the underlying input stream. To enable effective conversion from byte to character, you can read more byte from the bottom layer in advance, so that it exceeds the byte required for the current read operation.

To achieve the highest efficiency, inputstreamreader should be packaged in bufferedreader.

For example:

Bufferedreader in = new bufferedreader (New inputstreamreader (system. In ));

The biggest feature of inputstreamreader is that it can be converted to a fixed encoding format, which is not supported by other classes. From the constructor method
It can be seen that this is very useful when reading Chinese Characters
(4) filereader
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
Constructor Summary

FileReader

(File
 file)


Given the file from which data is read
Create a new filereader
.

FileReader

(FileDescriptor
 fd)


Given the filedescriptor from which data is read
Create a new
Filereader
.

FileReader

(String
 fileName)


Create a new filereader when the file name from which the data is read is specified.

 

The main difference between this class and its parent class inputstreamreader lies in the constructor. The main difference lies in the constructor!

From the constructor of inputstreamreader, we can see that the parameters are inputstream and the encoding method,

To specify the encoding method, you must use the inputstreamreader class. The parameters of the filereader constructor are the same as those of fileinputstream, which is a file object or a string that represents the path. We can see that, when you want to read a file based on a file object or string, use filereader. The role of the filereader subclass lies in this small division of labor.

2. Connection and Difference


(1) characters and Bytes:



Fileinputstream
The class is binary input/output, and the I/O speed is fast and efficient, but its Read () method reads a byte (binary data), which is not conducive to reading, in addition, you cannot directly perform operations on characters in the file, such as replacement and search (operations must be performed in the form of characters );
The reader class makes up for this defect and can be input/output in text format, which is very convenient. For example, you can use while (CH =
Filereader. Read ())! =-1
) To read the file cyclically. You can use the Readline () method of bufferedreader to read text in one row.

(2) Encoding


Inputstreamreader
It is a bridge between byte conversion and character conversion. You can specify the encoding method in the constructor. If you do not specify the encoding method, the underlying operating system uses the default encoding method, such as GBK.

Filereader and inputstreamreader
Involved in encoding conversion (specifying the encoding method or adopting OS default encoding), garbled characters may occur on different platforms! Fileinputstream
It is processed in binary format without garbled characters.

Therefore, to specify the encoding method, you must use inputstreamreader.
Class, so it is a bridge between byte conversion and character conversion;

(3) buffer zone


Bufferreader class is used to wrap all its Read ()
Reader with high overhead (such as filereader and inputstreamreader ).

(4) standard usage


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, "UTF-8 ");

Bufferedreader bufreader = new bufferedreader (inreader );

3) file = new file ("hello.txt ");

Filereader = new filereader (File );

Bufferedreader bufreader = new bufferedreader (filereader );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.