JavaSE 进阶(十五)

流(续)

字符流

字符流的父类(抽象类)

  • Reader(字符输入流)
  • Writer(字符输出流)

字符流的子类

文件字符流(自带缓冲区)
  • FileReader
    • int read():从输入流中读取一个字符数据。返回读到的字符数据,如果达到流末尾,返回-1。
    • int read(char[] c):从输入流中读取字符数组长度的字符数据存入数组中。返回实际读到的字符数,如果达到流末尾,返回-1。
  • FileWriter
    • void write(int n):将指定字符写入输出流,只能写入包含16位低阶字节的整型数值,16位高阶字节将会被忽略。
    • void write(String str):将指定字符串写入输出流。
    • void write(char[] cbuf):将指定字符数组写入输出流。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package ml.guest997;

import java.io.FileReader;
import java.io.IOException;

public class FR {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("test.txt");
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
fr.close();
}
}
//结果为 123abc你好
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package ml.guest997;

import java.io.FileWriter;
import java.io.IOException;

public class FW {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("out3.txt");
fw.write(110);
fw.write("Guest997");
fw.write("你好");
fw.close();
}
}
//输出的文件内容为 nGuest997你好
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package ml.guest997;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

//文件复制(只能复制文本文件,因为其它文件并没有字符编码,自然就无法正常读取和写入)
public class CopyFile2 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("test.txt");
FileWriter fw = new FileWriter("copy2.txt");
int data;
while ((data = fr.read()) != -1) {
fw.write(data);
}
fr.close();
fw.close();
}
}
//输出的文件内容为 123abc你好
字符缓冲流

BufferedReader / BufferedWriter

  • 高效读写
  • 支持换行输入符
  • 支持读一行或写一行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package ml.guest997;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BR {
public static void main(String[] args) throws IOException {
//方法一
FileReader fr1 = new FileReader("test2.txt");
BufferedReader br1 = new BufferedReader(fr1); //需要传入 Reader 的实现类
int data;
while ((data = br1.read()) != -1) {
System.out.print((char) data);
}
br1.close(); //只需关闭 br 即可,br 内部会关闭 fr。

System.out.println("\n------");

//方法二
FileReader fr2 = new FileReader("test2.txt");
BufferedReader br2 = new BufferedReader(fr2);
String lineData;
while ((lineData = br2.readLine()) != null) { //读取一行数据,读取到最后一行之后就为 null。
System.out.println(lineData);
}
br2.close();
}
}
/*结果为
456
def
世界
------
456
def
世界
*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package ml.guest997;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BW {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("out4.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Guest997");
bw.newLine(); //换行
bw.write("一个在互联网下的小小 Coder");
bw.close();
}
}
/*输出的文件内容为
Guest997
一个在互联网下的小小 Coder
*/
打印流

PrintWriter

  • 封装了 print() 和 println()。
  • 支持数据原样打印。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package ml.guest997;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class PW {
public static void main(String[] args) throws FileNotFoundException {
PrintWriter pw = new PrintWriter("print.txt");
pw.println(110);
pw.println("你好");
pw.println(3.1415926);
pw.println(true);
pw.close();
}
}
/*输出的文件内容为
110
你好
3.1415926
true
*/
转换流

InputStreamReader / OutputStreamWriter

  • 字节流和字符流互转。
  • 可设置字符的编码方式。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ml.guest997;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class ISR {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("test.txt");
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); //需要传入 InputStream 的实现类
int data;
while ((data = isr.read()) != -1) {
System.out.print((char) data); //之前使用 FileInputStream 读取中文时是会乱码的。
}
}
}
//结果为 123abc你好
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package ml.guest997;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

public class OSW {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("out5.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
osw.write(99);
osw.write("guest"); //之前使用 FileOutputStream 还需要转成字节数组,中文还需要设置编码。
osw.write("你好");
osw.close();
}
}
//输出的文件内容为 cguest你好