The character stream is optimized for the characteristics of character data.Thus providing some useful character-oriented features,The source or destination of a character stream is usually a text file. reader and writer are the parent classes for all character streams in the java.io package.Since they are all abstract classes,So you should use their subclasses to create entity objects,Use objects to handle related read and write operations.The subclasses of reader and writer can be divided into two categories:one is used to read data from a data source or write data to a destination (called a node stream), and the other performs some processing on the data (called Process stream).
The character-oriented input stream classes are all subclasses of reader, and their class hierarchy is shown in the figure.
The following table lists the main subclasses and descriptions of readers.
The reader provides the method shown in this table.You can use these methods to get bit data within a stream:
Reading files using the filereader class
The filereader class is a subclass of the reader subclass of the inputstreamreader class,So the filereader class can use either the reader class method 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 an object of class filereader.Then call the read () method. The format of the filereader constructor is:
public filereader (string name);//Create a readable input stream object based on the file name
[Example] Use 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 an array that can hold 1000 characters
filereader b=new filereader ("ep10_1.txt");
int num=b.read (a);//Read data into array a and return the number of characters
string str=new string (a, 0, num);//Convert string array to string
system.out.println ("The number of characters read is:" + num + ", the content is:\ n");
system.out.println (str);
}
}
have to be aware of is,java treats a kanji or english letter as a character,A carriage return or line feed is treated as two characters.
Reading files using the bufferedreader class
The bufferedreader class is used to read the data in the buffer.When used, you must create a filereader class object, and then create an object of the bufferedreader class with this object as a parameter.The bufferedreader class has two constructors,Its format is:
public bufferedreader (reader in);//Create buffer character input stream
public bufferedreader (reader in, int size);//Create an input stream and set the buffer size
[Example] Use the bufferedreader class to read the content 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) {//Read 1 line at a time
count ++;//Calculate the number of rows read
system.out.println (oneline);
}
system.out.println ("\ n read a total of" + count + "lines");
b.close ();
}
catch (ioexception io) {
system.out.println ("What went wrong!" + io);
}
}
}
have to be aware of is,When the read () or write () method is executed, the system may throw an ioexception exception due to an io error. You need to include the statement that performs the read and write operations in the try block and handle the possible exception through the corresponding catch block.
Related articles
- Method for converting byte stream into character stream in java programming
- Example of mutual conversion function between Java implementation file and base64 stream
- Java implementation of input stream into String
- Java byte stream and basic data type conversion example
- Explain the difference between character stream and byte stream in Java
- Detailed character-oriented output stream in Java programming
- Detailed description of java character stream buffer
- Byte and character stream IO operation example in Java programming
- Difference between Java character stream and byte stream for file operation
- Java character stream and byte stream difference and usage analysis
- Detailed explanation and examples of IO stream character stream in Java
- Java byte character conversion stream operation
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning