A detailed interpretation of character-oriented input stream _java in Java programming

Source: Internet
Author: User
Tags int size

A character stream is optimized for character data, thus providing some useful character-oriented characters, the source or target of which is usually a text file. Reader and writer are the parent classes of all character streams in the java.io package. Because they are abstract classes, you should use their subclasses to create entity objects that use objects to handle related read and write operations. The subclasses of reader and writer can be divided into two broad categories: one for reading data from a data source or for writing to a destination (called a node stream), and another for performing some sort of processing on the data (called a processing flow).

Character-oriented input stream classes are subclasses of reader, and their class hierarchies are shown in the figure.

The following table lists the main subclasses and descriptions of Reader.

The method provided by Reader, as shown in this table, can be used to obtain bit data in the stream:

reading files using the FileReader class

The FileReader class is a subclass of the reader subclass InputStreamReader class, so the FileReader class can use either the method of the reader class or the InputStreamReader class method to create the object.

When reading a file using the FileReader class, you must first call the FileReader () constructor to create the object of the FileReader class, and then call the Read () method. The format of the FileReader construction method is:

  Public FileReader (String name); Create a readable input stream object based on the filename

"Example" uses the FileReader class to read the contents of a plain text file

Import java.io.*;
Class ep10_1{public
  static void Main (String args[]) throws ioexception{
    char a[]=new char[1000];//create can hold 1000 characters The array
    filereader b=new filereader ("Ep10_1.txt");
    int Num=b.read (a); Reads the data into array A and returns the number of characters
    string str=new string (a,0,num);//Convert the string array to a string
    System.out.println ("read the number of characters: +num+", Content is: \ n ");
    System.out.println (str);
  }



It should be noted that Java treats a Chinese character or an English letter as a character, and a carriage return or newline is treated as two characters.
reading files using the BufferedReader class

The BufferedReader class is used to read data in a buffer. You must create a FileReader class object when you use it, and then create an object of the BufferedReader class for the parameter. The BufferedReader class has two constructed methods in the form of:

  Public BufferedReader (Reader in); Create a buffer character input stream public
  BufferedReader (Reader in,int size);//create an input stream and set the buffer size

"Example" uses the BufferedReader class to read the contents of a plain text file

Import java.io.*;
Class ep10_2{public
  static void main (string args[]) throws ioexception{
    String oneline;
    int count=0;
    try{
        filereader a=new filereader ("Ep10_1.txt");
        BufferedReader b=new BufferedReader (a);
        while ((Oneline=b.readline ())!=null) {//per read 1 rows
           count++;//Compute the number of rows read
           System.out.println (oneline);
        }
        System.out.println ("\ n Read the" +count+ "line");
        B.close ();
      }
      catch (IOException io) {
        System.out.println ("Wrong!") +io);}}


Note that when you execute the read () or write () method, you may have an IO error, the system throws a IOException exception, the statement that performs the read-write operation is included in the try block, and the possible exceptions are handled through the appropriate catch block.

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.