IO流的分类:
- 按照数据流向 站在内存角度
输入流 读入数据
输出流 写出数据 - 按照数据类型
字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件
1.字节输入流的继承体系(常用的)
InputStream的功能 | 概述 |
---|---|
read(byte[] b, int off, int len) | 从流中从off的位置开始读取len个字节的数据存储到b中,返回结果是实际读取到的字节个数(当再次读时,如果返回-1说明到了结尾,没有了数据) |
int read(byte[] b) | 一次读取一个字节数组 |
Close() | 关闭流,释放资源 |
(1)FileInputStream流
其特有的方法 | 概述 |
---|---|
read() | 一次读取一个字节 |
public class MyTest2 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("a.txt");
//创建一个空的字节数组,充当缓冲区
byte[] bytes = new byte[1024];
//返回值是他读取到的有效的字节个数
int len = in.read(bytes);
for (byte aByte : bytes) {
System.out.println(aByte);
}
System.out.println(len);
String s = new String(bytes,0,len);
System.out.println(s);
//释放资源
in.close();
}
}
(2)ByteArrayInputStream内存操作流
private static void read(byte[] bytes) throws IOException {
/* ByteArrayInputStream( byte[] buf)
创建一个 ByteArrayInputStream,使用 buf 作为其缓冲区数组。*/
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
byte[] bytes1 = {(byte) read, (byte) read1, (byte) read2};
String s = new String(bytes1);
System.out.println(s);
注意:
内存操作流:这个流不关联任何文件,只能在内存中数据
(3)ObjectInputStream 反序列化流 读取对象
对对象进行读取
private static void readObj() throws IOException, ClassNotFoundException {
ObjectInputStream objIN = new ObjectInputStream(new FileInputStream("student.txt"));
Object o = objIN.readObject();
Student student= (Student) o;//转型
System.out.println(student.getName());
System.out.println(student.getAge());
2.字节输出流的继承体系(常用的)
OutputStream类的方法 | 概述 |
---|---|
write(byte[] b) | 将b的长度个字节数据写到输出流中。 |
write(byte[] b,int off,int len) | 从b的off位置开始,获取len个字节数据,写到输出流中。 |
flush() | 刷新输出流,把数据马上写到输出流中。 |
close() | 关闭流释放资源 |
(1)FileOutputStream流
public class MyTest2 {
public static void main(String[] args) throws IOException {
//默认的是,你每次运行,都会覆盖之前文件中的数据
/* FileOutputStream(File file, boolean append)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。
*/
//参数2:true 代表追加写入,不会重新覆盖文件中之前的数据
FileOutputStream out = new FileOutputStream("e.txt",true);
out.write("江畔何人初见月".getBytes());
out.write("\r\n".getBytes());
out.write("江月何年初照人".getBytes());
out.write("\r\n".getBytes());
out.close();
}
}
(2)ByteArrayOutputStream内存操作流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write("好好学习".getBytes());
bos.write("天天向上".getBytes());
bos.write("爱生活".getBytes());
bos.write("爱Java".getBytes());
byte[] bytes = bos.toByteArray();
read(bytes);
(3)ObjectOutputStream序列化流 存储对象
private static void wiriteObj() throws IOException {
Student student = new Student("张三", 23);
ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream("student.txt"));
objOut.writeObject(student);
objOut.close();
}
}
3.字符输入流的继承体系
Reader流的方法 | 概述 |
---|---|
read() | 读取单个字符,返回结果是一个int,需要转成char;到达流的末尾时,返回-1 |
read(char[] c) | 读取c的长度个字符,返回结果是读取的字符数 |
close() | 关闭流,释放占用的系统资源。 |
(1)InputStreamReader
其特有的方法 | 概述 |
---|---|
read(char[] cbuf, int offset, int length) | 从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1 |
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
char[] chars = new char[1024];
int len = in.read(chars); //返回值是读取到的有效字符个数
//把字符数组转换成字符串
String s = new String(chars, 0, len);
System.out.println(s);
(2)BufferedReader
其特有的方法 | 概述 |
---|---|
read(char[] cbuf, int offset, int length) | 从offset位置开始,读取length个字符到cbuf中,返回结果是实际读取的字符数,到达流的末尾时,返回-1 |
readLine() | 读取一个文本行,以行结束符作为末尾,返回结果是读取的字符串。如果已到达流末尾,则返回 null |
public class MyTest2 {
public static void main(String[] args) throws IOException {
/* A:
案例演示:
需求:从文本文件中读取数据(每一行为一个字符串数据) 到集合中,并遍历集合*/
BufferedReader bfr = new BufferedReader(new FileReader("name.txt"));
ArrayList<String> list = new ArrayList<>();
while (true) {
String s = bfr.readLine();
if (s == null) {
break;
}
list.add(s.trim());
}
System.out.println(list);
}
}
4.字符输出流的继承体系
Writer流的方法 | 概述 |
---|---|
write(char[] cbuf) | 往输出流写入一个字符数组。 |
write(int c) | 往输出流写入一个字符。 |
write(String str) | 往输出流写入一串字符串 |
write(String str, int off, int len) | 往输出流写入字符串的一部分。 |
close() | 关闭流,释放资源。 |
flush() | 刷新输出流,把数据马上写到输出流中。 |
(1)OutputStreamWriter
public class MyTest2 {
public static void main(String[] args) throws IOException {
//参数2 可以指定码表 GBK UTF-8
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.txt",true),"UTF-8");
out.write("我们使用GBK编码写入");
out.write("\r\n");
out.write("我们使用GBK编码写入");
out.write("\r\n");
out.flush();//字符流记得刷新
//释放资源
out.close();
}
}
(2)BufferedWriter
其特有的方法 | 概述 |
---|---|
newLine() | 写 入一个换行符 |
public class MyTest {
public static void main(String[] args) throws IOException {
/*A:
案例演示:
需求:把ArrayList集合中的字符串数据存储到文本文件*/
ArrayList<String> list = new ArrayList<>();
list.add("张飞");
list.add("赵云");
list.add("马超");
list.add("关羽");
list.add("黄忠");
list.add("邢道荣");
BufferedWriter bfw = new BufferedWriter(new FileWriter("name.txt"));
for (String s : list) {
bfw.write(s);
bfw.newLine();
bfw.flush();
}
bfw.close();
}
}