1. Character Stream
In the program a character equals two bytes, Java provides us with reader and writer two classes dedicated to manipulating character streams
1) Character output stream: Writer
Writer is a character stream, which is an abstract class, so to use it, it must also be instantiated by its subclass before it can be used.
common methods of writer class
Method Name |
Description |
Public abstract void Close () throws IOException |
Turn off the output stream |
Public void Write (String str) throws IOException |
Output a string |
Public void Write (char cbuf) throws IOException |
Output a character array |
Public abstract void Flush () throws IOException |
Mandatory empty Cache |
Example 1:helloworld
Writing data to a text file through a character output stream
[Java] View Plain copy print?
- public static void Main (string[] args ) throws Exception {
-
- //Declare a file object
-
- File File = new file ("Hellowolrd.txt");
-
-
-
- //Declare a Write Object
-
- writer writer = null;
-
- //Use the FileWriter class to instantiate an object of the writer class and write it in an appended form
-
- writer = new FileWriter (file, true);
-
-
-
- //Declare a string to write
-
- string str = "Write HelloWorld in string form";
-
- //write in a text file
-
- writer.write (str);
-
- //Refresh
-
- Writer.flush ();
-
- //close character output stream
-
- Writer.close ();
-
- }
2) character input stream: Reader
Reader itself is also an abstract class, and similarly, if you use it, we need to instantiate it by its subclass to use it.
Common methods of reader class
Method Name |
Description |
Public abstract void Close () throws IOException |
|
public int Read () throws IOException |
|
public int Read (char cbuf) throws IOException |
|
Through the method we see that the reader class only provides a way to read characters
Example 2: or HelloWorld
Read the contents of the text on top of it and display it on the console
[Java] View Plain copy print?
- public static void Main (string[] args) Throws Exception {
-
- //Declare a file object
-
- File File = new file ("Hellowolrd.txt");
-
-
-
- //Declare an object of a reader class
-
- Reader reader = null;
-
-
-
- //Instantiate reader object by FileReader subclass
-
- reader = new filereader (file);
-
-
-
- //Declare a character array
-
- char[] c = new char[1024x768];
-
- //// outputs the content
-
- //int len = Reader.read (c);
-
-
-
- //Loop mode one read
-
- int len=0;
-
- int temp=0;
-
- While ((Temp=reader.read ())!=-1) {
-
-
-
- c[len]= (char) temp;
-
- len++;
-
- }
-
- //Close input stream
-
- Reader.close ();
-
- //Convert char array to string output
-
- System.out.println (new String (c, 0, Len));
-
-
-
- }
2. The difference between a character stream and a byte stream
The operation of the byte stream operation does not use the buffer itself, it is the direct operation of the file itself, and the byte stream is used to buffer the operation.
If we do not close the stream while manipulating the character stream, the data we write cannot be saved. So you must remember to close the stream when you manipulate the character stream.
Character stream of Java io