Character Stream
The byte stream provides the ability to handle any type of input/output operation (because everything is 0 and 1 for a computer, it is enough to simply represent the data in bytes), but they cannot manipulate Unicode characters directly because the previous article wrote that a Unicode character occupies 2 bytes, The byte stream can operate only one bytes at a time. Since Java's slogan is "Write once, run everywhere", it is necessary to include direct character input/output support. So there are some character input/output streams that have been explained before, and the top layer of the character stream is the two abstract classes of reader and writer, so start this article here.
Reader
Reader is an abstract class that defines the character input stream of Java, and all methods of that class will throw ioexception in case of an error. These methods are available in the reader class:
Method |
Role |
abstract void Close () |
Closes the stream and frees all resources associated with it |
void mark (int readaheadlimit) |
Marks the current position in the stream |
Boolean marksupported () |
Determine if this stream supports the mark () operation |
int read () |
Reading a single character from a file |
int read (char[] cbuf) |
Reading characters from a file to Cbuf |
abstract int Read (char[] cbuf, int off, int len) |
Reads the characters from the file into the Cbuf array, starting at the off position and reading Len characters. Three read methods This method is blocked until the character is available, an I/O exception occurs, or the end of the stream has been reached |
int read (Charbuffer target) |
An attempt was made to read a character in a file into a specified character buffer |
Boolean Ready () |
Determine if this stream is ready to be read |
Voi Reset () |
Reset the Stream |
Long Skip (long N) |
Skip N Characters |
Writer
Writer is an abstract class that defines a character output stream, and all methods of that class return a void value and raise ioexception in the condition of an error. The methods in the writer class are:
Method |
Role |
Writer append (char c) |
Add the enactment character to this writer |
Writer Append (charsequence csq) |
Add a sequence of characters to this writer |
Writer Append (charsequence csq, int start, int end) |
Adds a subsequence of the specified character sequence to this writer. Appendable |
abstract void Close () |
Close the stream, but first flush () it |
abstract void Flush () |
Refreshes the buffer of the stream |
void Write (char[] cbuf) |
Writing content in Cbuf to a file |
abstract void Write (char[] cbuf, int off, int len) |
Writes Len bytes from off in the character array cbuf to a file |
void write (int c) |
Write a single character to a file |
void write (String str) |
Write String to File |
void Write (String str, int off, int len) |
Writes the Len characters from the off position of STR to a file |
FileReader and FileWriter
The FileReader class creates a reader class that can read the contents of a file, most commonly constructed by:
1. FileReader (String fileName)
2, FileReader (file file)
FileWriter creates a writer class that can write files, the most commonly constructed methods are:
1. FileWriter (String fileName)
2, FileWriter (String fileName, Boolean append)
3, FileWriter (file file)
The second construction method, If Append is true, then the output is appended to the end of the file . the creation of the FileWriter class does not depend on the existence of the file, and FileWriter will open it as output when the object is created before the file is created. If you try to open a read-only file, a ioexception is raised. Look at the use of FileWriter and FileReader, now there is no "writer.txt" in the D-Disk directory:
public static void Main (string[] args) throws exception{ File File = new file ("D:/writer.txt"); Writer out = new FileWriter (file); Declares a String type Object string str = "Hello world!!!"; Out.write (str); Out.close (); Read file operation Reader in = new FileReader (file); Open up a space for receiving files read in data char c0[] = new char[1024]; int i = 0; Pass the C0 reference to the Read () method, and this method returns the number of read data i = In.read (C0); In.close (); if ( -1 = = i) { System.out.println ("No data in File"); } else { System.out.println (new String (C0, 0, I));} }
Using FileWriter and FileReader to do a read and write operation, first look at the D disk under the "Writer.txt", if there is "writer.txt" that "Writer.txt" is what:
It appears that using FileWriter to write an in-memory string to the file is successful, then use the FileReader to read the contents of the file into memory, and look at the results of the operation:
Hello World!!!
The print results are consistent with the contents of the file, indicating that the FileReader operation was successful.
This is the use of FileWriter and FileReader, and the use of FileOutputStream and FileInputStream is similar, but the actual operation is generally not used filewriter and FileReader
Java base--io Stream character streams