Note: This article is the basic knowledge for beginners.
Character Stream: Characters are read. It is divided into input stream and output stream.
Reader: The character input stream is an abstract class. Is a superclass of all character input streams.
Writer: The character output stream is an abstract class. Is a superclass of all character output streams.
1. FileReader of Reader
Common methods:
int read()
Read a single character. Returns characters read as integers, ranging from 0 to 65535 (0x00-0xffff), and - 1 if it reaches the end of the stream.
int read(char[] cbuf)
Read characters into an array. Returns the number of characters read, and - 1 if it reaches the end of the stream.
int read(char[] cbuf, int off, int len)
Read characters into a part of an array. Returns the number of characters read, and - 1 if it reaches the end of the stream
long skip(long n)
Skip characters.
void close()
Close the stream and release all resources associated with it.
@Test
public void testReader() throws IOException {
//abcdefg is stored in hello.txt
//Construction method
//FileReader(File file)
//FileReader(String name)
FileReader reader = new FileReader("D:\\hello.txt");
System.out.println("---------1--------");
while(true) {
//Read a character at a time
int c = reader.read();
if(c== -1) {
break;
}
System.out.println(c);
}
reader.close();
//Operation result
/*
---------1--------
97
98
99
100
101
102
103
*/
System.out.println("---------2--------");
FileReader reader1 = new FileReader("D:\\hello.txt");
while(true) {
//Read more than one character at a time and put the read character into an array of characters.
char[] chars = new char[10];
int len = reader1.read(chars);
if(len == -1) {
break;
}
System.out.println(len);
System.out.println(Arrays.toString(chars));
}
reader1.close();
//Operation result
/*
---------2--------
7
[a, b, c, d, e, f, g,
*/
System.out.println("---------3--------");
FileReader reader2 = new FileReader("D:\\hello.txt");
while(true) {
//Read more than one character at a time and put the read character into an array of characters.
char[] chars = new char[10];
int len = reader2.read(chars, 2, 8);
if(len == -1) {
break;
}
System.out.println(len);
System.out.println(Arrays.toString(chars));
}
reader2.close();
//Operation result
/*
---------3--------
7
[ , ,a, b, c, d, e, f, g, ]
*/
System.out.println("---------4--------");
FileReader reader3 = new FileReader("D:\\hello.txt");
while(true) {
char[] chars = new char[3];
int len = reader3.read(chars);
if(len == -1) {
break;
}
reader3.skip(2);
System.out.println(Arrays.toString(chars));
System.out.println(len);
}
reader3.close();
//Operation result
/*
---------4--------
[a, b, c]
3
[f, g, ]
2
*/
}
2. FileWriter of Writer
Common methods:
Writer append(char c)
Adds the specified character to this writer.
void write(int c)
Write a single character. The characters to be written are contained in 16 low bits of a given integer value, i.e. between 0 and 65535.
void write(char[] cbuf)
Write to an array of characters.
void write(char[] cbuf, int off, int len)
Writes to a part of the character array.
void write(String str)
Write a string.
void write(String str, int off, int len)
Write to a part of the string.
void close()
Close the stream, but refresh it first.
void flush()
Refresh the buffer of the stream.
@Test
public void testWriter() throws IOException {
FileWriter out = new FileWriter("D:\\helloworld.txt",true);
char c = 'a';
//The effect of the following two methods is the same. You can write a character.
out.append(c);//Write out a
out.write(c);//Write out a
//Write out an array of characters
char[] chars = {'a','b','c','d'};
out.write('-');//Write out -
out.write(chars);//Write out abcd
//Write out part of an array of characters
out.write('-');//Write out -
out.write(chars, 2, 2);//Write out cd
//Write out a string
String str = "abcd";
out.write('-'); //Write out -
out.write(str); //Write out abcd
//Write out a part of a string
out.write('-'); //Write out -
out.write(str,2,2); //Write out cd
out.close();
}
3.BufferedReader character buffer input stream
Construction method:
BufferedReader(Reader reader)
BufferedReader(Reader reader, int size) sizeBuffer size
Common methods:
void close()
Close the stream and release all resources associated with it.
int read()
Read a single character.
int read(char[] cbuf, int off, int len)
Read characters into a part of an array.
long skip(long n)
Skip characters.
The above method is the same as the previous one, and it will not be repeated here.
String readLine()
Read a line of text. A line can be considered terminated by one of the following characters: newline (' n'), return (' r') or directly follow the newline after return.
Returns a string containing the contents of that line, without any line terminators, and null if it reaches the end of the stream.
@Test
public void testBufferedReader() throws IOException {
FileReader reader = new FileReader("D:\\helloworld.txt");
BufferedReader buffR = new BufferedReader(reader);
while(true) {
String str = buffR.readLine();//At the end, null is returned.
if(str == null) {
break;
}
System.out.println(str);
}
buffR.close();
}
4. Output stream of Buffered Writer Character Buffer Stream
Construction method:
BufferedWriter(Writer out) Create a buffer character output stream using the default size output buffer. BufferedWriter(Writer out, int sz) Create a new buffer character output stream using a given size output buffer.
Common methods:
void close()
Close the stream, but refresh it first.
void flush()
Refresh the buffer of the stream.
void write(char[] cbuf, int off, int len)
Writes to a part of the character array.
void write(int c)
Write a single character.
void write(String s, int off, int len)
Write to a part of the string.
// The above method refers to the preceding.
void newLine()
Write a line separator.
@Test
public void testBufferedWriter() throws IOException {
FileWriter writer = new FileWriter("D:\\hello.txt",true);
BufferedWriter buffW = new BufferedWriter(writer);
String s1 = "ab";
buffW.write(s1);
buffW.newLine();//It acts as a new line.
buffW.write(s1);
buffW.close();
}