Java Character Stream
Character Stream
Among the classes in the java.io package, character stream-related classes are as follows.
- Reader
- BufferedReader
- InputStreamReader
- FileReader
- Writer
- BufferedWriter
- OutputStreamWriter
- FileWriter
Reader/Writer Abstract Classes
Because the Writer and Reader classes are abstract classes, actual objects cannot be created from them.
Reader Abstract Class
The Reader class provides character stream input functionality.
Main Reader Methods
| Method | Description |
|---|---|
| abstract void close() throws IOException | Closes the character input stream. |
| void mark(int limit) throws IOException | Marks the current position of the character input stream. |
| boolean markSupported() | Returns whether the character stream supports the mark() method. |
| int read() throws IOException | Reads a single character from the character input stream. |
| int read(char buf[]) throws IOException | Reads up to the size of buf[] from the character input stream, stores it in buf, and returns the number of characters read. |
| abstract int read(char buf[], int off, int len) throws IOException | Reads len characters from the character input stream, stores them at off in buf[], and returns the number of characters read. |
| int read(CharBuffer target) throws IOException | Reads a string into target, which is a CharBuffer. |
| boolean ready() throws IOException | Returns whether the character input stream is ready. |
| void reset() throws IOException | Returns the character input stream to the marked position. |
| long skip(long l) throws IOException | Skips the given number l of characters. |
Writer Abstract Class
The Writer class provides character stream output functionality.
Main Writer Methods
| Method | Description |
|---|---|
| Writer append(char c) throws IOException | Adds Character c to the Writer. |
| Writer append(CharSequence csq) throws IOException | Adds CharSequence csq to the Writer. |
| Writer append(CharSequence csq, int start, int end) throws IOException | Adds characters from start to end of CharSequence csq to the Writer. |
| abstract void close() throws IOException | Closes the character output stream. |
| abstract void flush() throws IOException | Outputs the remaining output stream in the buffer. |
| void write(String str) throws IOException | Outputs the given string str. |
| void write(char buf[]) throws IOException | Outputs the contents of buf. |
| void write(char buf[], int off, int len) throws IOException | Outputs len characters from the off position of buf. |
| void write(String str, int off, int len) throws IOException | Outputs len characters from the off position in the given string str. |