一、IO流
1.FileOutputStream(字节输出流)
用于写入数据
构造:
FileOutputStream fos = new FileOutputStream(String pathName);
FileOutputStream fos = new FileOutputStream(File file);
常用方法:
fos.write(int c);
fos.write(byte[] b); // 写一个字节数组
fos.write(byte[] b, int off, int len); // off: 开始的索引 len: 写的长度
fos.write(“字符串”.getBytes());
使用流后需要释放资源
示例:
FileOutputStream f = new FileOutputStream("test.txt");
byte[] b = {0, 1, 2, 3, 4, 5};
f.write(100);
f.write(b);
f.write(b, 0, 3);
f.close(); // 关闭流
2.FileInputStream(字节输入流)
用于读取数据
构造:
FileInputStream fis = new FileInputStream(String pathName);
FileInputStream fis = new FileInputStream(File file);
常用方法:
fis.read(); // 读取一个字节的数据,读取到的数据封装到返回值int
fis.read(byte[] b); // 读取一个字节数组的数据,读取到的数据封装在一个字节数组中,返回读取到的有效数据的个数
3.BufferedOutputStream(缓冲字节输出流)
构造方式及常用方法与字节输出流相同,效率高于字节输出流
4.BufferedInputStream(缓冲字节输入流)
构造方式及常用方法与字节输入流相同,效率高于字节输入流
5.OutputStreamWriter(原生字符输出流(转换流))
将指定的字节流转换成字符流时使用
构造:
- OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream(String pathName/File file)); - OutputStreamWriter osw = new OutputStreamWriter(new
FileOutputStream(String pathName/File file), String characterName);
常用方法:
- osw.write(int c);
- osw.write(char[] c);
- osw.write(char[] c, int off, int len);
- osw.write(String str); // 写一个字符串的数据到文件中
- osw.write(String str, int off, int len); // 写一个字符串的一部分
- osw.flush(); // 将缓冲区的数据刷新到文件中
6.InputStreamWriter(原生字符输入流(转换流))
构造:
- InputStreamWriter isw = new InputStreamWriter(new
FileInputStream(String pathName/File file)); - InputStreamWriter isw = new InputStreamWriter(new
FileInputStream(String pathName/File file), String characterName);
常用方法:
-
isw.read(); // 读取一个字符的数据,读取到的数据封装到int返回值中
-
isw.read(char[] c); //
读取一个字符数组的数据,读取到的数据封装到一个char[]中,读取到的有效数据长度封装在返回值中
7.FileWriter(便捷字符输出流)
构造:
FileWriter fw = new FileWriter(String pathName/File file);
FileWriter fw = new FileWriter(String pathName/File file, boolean append);
常用方法:
- fw.write(int c);
- fw.write(char[] c);
- fw.write(char[] c, int off, int len);
- fw.write(String str);
- fw.write(String str, int off, int len);
- fw.flush() // 将缓冲区的数据刷新到文件中
8.FileReader(便捷字符输入流)
构造:
FileReader fr = new FileReader(String pathName/File file);
常用方法:
- fr.read();
- fr.read(char[] c);
9.BufferedWriter(缓冲字符输入流)
构造:
BufferedWriter bw = new BufferedWriter(new FileWriter(String pathName/File file));
常用方法:
-
bw.write(int c);
-
bw.write(char[] c); - bw.write(char[] c, int off, int len);
-
bw.write(String str);
-
bw.write(String str, int off, int len);
-
bw.newline(); 输出换行符
-
bw.flush(); 将缓冲区的数据输出到文件中
10.BufferedReader(缓冲字符输入流)
构造:
BufferedReader br = new BufferedReader(new FileReader(String pathName/File file));
常用方法:
- br,read();
- br.read(char[] c);
- br.readline(); 读取一行数据,返回一个String字符串,不包括结尾的换行符
11.System.in(标准输入流)
本质是一个字节流,可以获取键盘输入的数据
OutputStreamReader osr = new OutputStreamReader(System.in); 使用转换流将输入的字节流转换成字符流
12.System.out(标准输出流)
本质是一个字节流
13.PrintStream(字节打印流)
构造:
PrintStream ps = new PrintStream(String pathName/File file);
常用方法:
- ps.print(“”);
- ps.println();
14.PrintWriter(字符打印流)
构造:
PrintWriter pw = new PrintWriter(String pathName/File file);
常用方法:
- ps.print(“”);
- ps.println();
- 在调用println/printf/format三个方法时自动刷新
15.ObjectOutputStream(序列化流)
构造及方法:
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(String pathName/File file))
oos.writeObject(Object o); // 将一个对象序列化到文件中
16.ObjectInputStream(反序列化流)
构造及方法:
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(String pathName/File file))
ois.readObject(); // 读取文件中的一个对象