Java File Processing Methods (fileinputstream, filereader, file *)

Source: Internet
Author: User

Use and difference of fileinputstream, filereader, inputstreamreader file, fileinputstream, filereader, inputstreamreader, and bufferedreader
References:
Chapter 12 of core Java
Detailed description of http://java.ccidnet.com/art/3737/20041108/523627_1.html using Java to operate text files
What is filereader class? What is the difference with fileinputstream ???
Http://book.hackbase.com/ask2/ask107572.htm

Organize and comprehend by yourself:

Introduction:
The C language only needs a file *. Unlike C, Java has a series of stream types with more than 60 types. The designer of the class library claims: "There is enough reason to provide users with a wide selection of stream types: this can reduce program errors ." For example, in C language, many people think that "writing the 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. 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 retrieve the time when the file was last modified, 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 (), 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: (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 that some file systems are 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 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. "!

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?
Readers and writers work only on line based character data, so plain text files.
For anything else, you must use streams.
JDK 5 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: reads data in byte streams; filereader: converts a file to a bytes stream for reading;
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 bytes stream reading char (16-bit, Unicode encoding). inputstream and its subclass provide the byte stream reading byte (8-bit ), therefore, the filereader class reads files in bytes streams, while fileinputstream reads files in byte streams. inputstreamreader can convert read data such as stream into bytes streams, which is 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 (binary data), 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 you do not specify the encoding method, the underlying operating system uses the default encoding method, 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.
3) Understanding:
If you are dealing with plain text files, we recommend that you use filereader, which 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!

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:
Understanding:
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. We can see that filereader is used to read a file based on the file object or string. I think the role of the filereader subclass lies in this small division of labor.
3) 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. }

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
JDK 5 API:
Read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
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:
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. 1) file = new file ("hello.txt ");
  2. Fileinputstream in = new fileinputstream (File );

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

  7. 3) file = new file ("hello.txt ");
  8. Filereader = new filereader (File );
  9. Bufferedreader bufreader = new bufferedreader (filereader );

 

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.