The byte stream in Java cannot directly operate the Unicode character, you want to directly operate the character input / output to use several characters input / output classes.
The top layer of the character flow hierarchy is Reader and Writer abstraction classes.
1, Reader
Reader is an abstract class that defines the streaming character input mode of Java. Error exception is IOException.
The main methods are:
Abstract Void Close () Close the input stream, and read the ioException exception after closing.
Void Mark (int NumChars) sets a flag at the current location of the input stream. This input stream is valid before the NumChars characters are read.
Boolean Marksupported () This stream supports mark () / reset () returns true.
INT read () Returns an integer if the next character called the input stream returns a file end returns -1.
Int Read (Char Buffer []) attempts to read the buffer.length character in the buffe, returns the number of characters actually successfully read, and the file returns -1.
Abstract Int Read (CHAR BUFFER [], int Offset, int Numchars Attempts to read the NumChars character starting from buffer in Buffer, returning the number of characters actually successfully read, the file is returned -1.
Void reset () Sets the input pointer to the previously established flag.
Long Skip (Long Numchars) Skip NumChars an input character, returns the number of skips.
2, Writer
Writer is an abstract class that defines the output of the stream. All such methods return a VOID value and trigger an IOException in an error condition.
Main method:
Abstract void close () Turn off the output stream, and the write operation after the shutdown triggers an oException exception.
Abstract Void Flush (0 custom output status so that each buffer is cleared. That is, refresh buffer.
Void Write (int CH) writes a single character to the output stream. The parameter is an integer, and it is possible to call the parameter to convert the parameter into a word.
Void Write (Char Buffer []) writes a full character array to an output stream.
Abstract Void Write (CHAR BUFFER [], int Offset, int Numchars is written to the array buffer in the number of contents of the NumChars character area of the call to buffer.
Void Write (String Str) writes STR to the output of the call.
Void Write (String Str, int offset, int Numchars) Write an array STR in the length of the starting point of the starting point as the content of the NumChars character area.
3, FileReader
The FileReader class creates an Reader class that can read the contents of the file. Its constructor:
FileReader (String FilePath) FilePath is a full path to the full path
FileReader (file fileobj) fileobj is a file object describing the file.
Running the filenotfoundexception exception.
For example, read from one file and output it to a standard input stream.
Import java.io. *;
Class FileReaderDemo {
Public static void main (string args []) throws exception {
FileReader fr = new fileReader ("c: //in.txt");
BufferedReader Br = New BufferedReader (FR);
String S;
While ((s = br.readline ())! = null) {system.out.prinln (s);
}
Fr.close ();
}
}
4, FileWriter
FileWriter Creates a Writer class that can write files. Constructor:
FileWriter (String Filepath) FilePath is a full path to a file
FileWriter (String FilePath, Boolean Append) If append is true, the output is attached to the file tail.
FileWriter (file fileobj) is a File object that describes the file.
For example, a sample character buffer has begun to generate a string, then use the getchars () method to extract the character array. Then create three files, file1.txt contains the separation character in the example, file2.txt contains all characters, file2.txt contains the last quarter.
Import java.io. *;
Class FileWriterdemo {
Public static void main (string args []) throws exception {
String Source = "Learn How to Write To File";
FileWriter F1 = New FileWriter ("File1.txt");
For (int i = 0; i F1.write (buffer [i]); } fo.close (); FileWriter F2 = New FileWriter ("File2.txt"); F2.write (buffer); f2.close (); FileWriter F3 = New FileWriter ("file3.txt"); F2.write (buffer, buffer.length-buffer.length / 4, buffer.length / 4); } }