Repeat: differences between fileinputstream and filereader

Source: Internet
Author: User

Differences between fileinputstream and filereader

Core tips: 1) File class introduction File class encapsulates the function of operating the file system on the user's machine. For example, you can use the File class to obtain the last modification time, move the object, or delete or rename the object. In other words, the stream class focuses on the file content, while the file class focuses on the main methods for storing files on disks: (), Last

1) File class Introduction

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 obtain the last modification time, move the object, or delete or rename the object. 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 (), lastmod: getname (), getcanonicalfileified (), isderector (), isfile (), and getpath;

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:

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 that some file systems are case sensitive, and the "/" character at the end of the directory does not matter.

The fileinputstream class or the filereader class has multiple constructors. The typical two are: one using the file object as the parameter, and the other using the string object representing the path as the parameter; I used to think that I could simply use string to specify the path. I still don't understand why many people construct a file object first. Now I understand, "If you process the file or directory name, you should use a file object instead of a string. "!

 

Fileinputstream class

1) fileinputstream class introduction:

Stream processing in bytes. Byte sequence: 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?

Fileinputstream: read in byte stream mode;

Filereader: converts a file into a readable stream;

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 reader class and its subclass provide the reading char of the bytes stream, and the inputstream and its subclass provide the reading byte of the byte stream. Therefore, the filereader class reads the file in the form of the bytes stream, fileinputstream reads files in byte streams. inputstreamreader can convert read data such as stream into bytes streams, which serves as a bridge between reader and stream.

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

The fileinputstream class uses binary input/output. I/O is fast and efficient, but its Read () method reads a byte, which is not conducive to reading. The filereader 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) read the file cyclically. You can use the Readline () method of bufferedreader to read text in one row. When reading and writing text files, It is very convenient to use reader, such as filereader, inputstreamreader, and bufferedreader. The most important class is inputstreamreader, which serves as a bridge between byte conversion and character conversion. You can specify the encoding method in the constructor. If not specified, the default encoding method of the underlying operating system will be used, such as GBK. Filereader and inputstreamreader involve encoding conversion (specify the encoding method or use OS default encoding). garbled characters may occur on different platforms! Fileinputstream is processed in binary format without garbled characters. If you are dealing with plain text files, we recommend that you use filereader because it is more convenient and suitable for reading. But pay attention to the encoding problem! In other cases (processing non-plain text files), fileinputstream is the only choice. fileinputstream is used for socket communication. For example, it is used to transmit the file stream to the server!

 

Filereader class

Introduction to the filereader class:

Subclass of the inputstreamreader class. All methods (such as read () are inherited from the parent class inputstreamreader;

Differences from the inputstreamreader class:

The main difference between this class and its parent class inputstreamreader lies in the constructor. The main difference lies in the constructor! The inputstreamreader constructor shows that the parameters are inputstream and the encoding method. It can be seen that the inputstreamreader class must be used to specify the encoding method. The parameters of the filereader constructor are the same as those of fileinputstream, it is a file object or a string that represents a path. When you want to read a file based on the file object or string, use filereader. I think the role of filereader subclass lies in this small division of labor.

General Usage:

  1. Filereader Fr = new filereader ("ming.txt ");
  2. Char [] buffer = new char [1024];
  3. Int CH = 0;
  4. While (CH = Fr. Read ())! =-1)
  5. {
  6. System. Out. Print (char) CH );
  7. }

 

Inputstreamreader class

Input/Output in text format. You can specify the encoding format;

Main Methods: getencoding (), read ();

General Usage:

  1. Inputstreamreader ISR = new inputstreamreader (New fileinputstream ("ming.txt "));
  2. While (CH = ISR. Read ())! =-1)
  3. {
  4. System. Out. Print (char) CH );
  5. }

 

Bufferedreader class

Bufferedreader is extended by the reader class and provides a general buffer mode for text reading. It also provides a very practical Readline, which is suitable for reading branch text. bufferedreader is designed for reader and not for files directly, it is not just for file reading.

General Usage:

  1. Bufferedreader BR = new bufferedreader (New inputstreamreader (New fileinputstream
  2. ("Ming.txt ")));
  3. String data = NULL;
  4. While (Data = Br. Readline ())! = NULL)
  5. {
  6. System. Out. println (data );
  7. }

Summarize the above content to get a better standard usage:

1)

  1. File file = new file ("hello.txt ");
  2. Fileinputstream in = new fileinputstream (File );
  3. Inputstreamreader inreader = new inputstreamreader (in );
  4. Bufferedreader bufreader = new bufferedreader (inreader );

2)

  1. Fileinputstream in = NULL;
  2. File file = new file ("hello.txt ");
  3. In = new fileinputstream (File );
  4. Bufferedreader bufreader = new bufferedreader (New inputstreamreader (in ));

3)

  1. File file = new file ("hello.txt ");
  2. 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 inputstreamreader object variable. It is directly added to the bufferedreader constructor. 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 you use the surround method (try & catch) for handling, you should use the second method. out. println indicates that the file is not found. Of course, throws exception is used after the function name, and the third method is also used, but it seems that this is suitable for user interface scenarios, throw the exception to the client for processing.

 

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.