1 IO
1.1 概述
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
I : input 输入 O : output 输出
输入 : 就是把数据输入到内存中
输出 : 就是把内存中的数据写出到外面
1.2 分类
1.2.1 按处理数据类型的不同,分为字节流和字符流
1.2.2 按数据流向的不同,分为输入流和输出流。(入和出是相对于内存来讲的)
1.2.3 按功能不同,分为节点流和处理流
节点流:直接操作数据源
处理流:对其他流进行处理
1.3 四大抽象类
InputStream : 字节输入
OutputStream : 字节输出
Reader : 字符输入
Writer : 字符输出
1.4 InputStream
1.4.1 read使用方式
read : 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1
FileInputStream是字节输入流,就是读取文件中的数据
* 操作系统层面一切皆文件
、、要读取文件,必须要找到它
//如何找? 有两种方式:绝对路径和相对路径
//绝对路径: 能够找到这个文件的全路径
//相对路径: 相对当前文件所在位置去找其他文件
// ./表示当前目录, ../表示上级目录, ../../表示上上级目录, ...
//FileInputStream fis=new FileInputStream("D:\\test\\a.txt");
//eclipse中 ./找到的是当前项目
FileInputStream fis=new FileInputStream("./src/com/FileInputStream_01.java");
//read: 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾返回-1
int i1=fis.read();
System.out.println((char)i1);
循环读取
try (FileInputStream fis = new FileInputStream(
"./src/com/IO_01_FileInputStream_01");) {
int temp = 0;
while ((temp = fis.read()) != -1) {
System.out.println((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
}
1.4.2 read重载使用方式
read方法的重载 , 可以传递一个字节数组,为了提高读取效率
默认一次读取一个字节,如果传递了字节数组,则把数组一次性读满,再回来,然后下次去再读满,一直到读完
read(byte[]) : 一次读取一个数组,返回当前次读取到的个数,如果到达文件末尾,返回-1
try(FileInputStream fis = new FileInputStream("./src/com/IO_01_FileInputStream_01.java");) {
//available: 可以获取的字节数
System.out.println(fis.available());
//byte[] bytes=new byte[fis.available()];
byte[] bytes=new byte[1025];
int count=fis.read(bytes);
System.out.println(new String(bytes,0,count));
System.out.println(count);
count=fis.read(bytes);
//new String(bytes)把自己数组中所有的数据转换为字符串,但可能有冗余数据
//所以推荐使用 new String(bytes,0,count)只把本次读取的数据转换为字符串
System.out.println(new String(bytes,0,count));
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
1.5 Reader
继承关系与使用方法和InputStream相同
FileReader字符输入流
* 字节输入流是一次读取一个字节,而字符输入流是一次读取一个字符
* 并且java中汉字采用的是unicode编码,而unicode编码占用16位,即是一个字符
* 所以使用字符输入流可以解决汉字乱码问题
* 字符流一般只用于纯文本文件,而压缩包,图片,视频等还是需要使用字节流处理
* read(): 读取一个字符,返回该字符的int值,到达文件末尾返回-1
* read(char[]): 读取一个char数组,返回当前读取的个数,到达末尾返回-1
1.5.1 read使用方式
try (FileReader fr = new FileReader(
"./src/com/IO_01_FileInputStream_01.java")) {
int temp = 0;
while ((temp = fr.read()) != -1) {
System.out.print((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
}
1.5.2 read重载使用方式
try (FileReader fr = new FileReader(
"./src/com/IO_01_FileInputStream_01.java")) {
char[] chars = new char[512];
int temp = 0;
while ((temp = fr.read(chars)) != -1) {
System.out.println(new String(chars, 0, temp));
}
} catch (Exception e) {
e.printStackTrace();
}
1.6 OutputStream
1.6.1 概述
上同
输出流是把内存中的数据写到硬盘中
* 如果该文件不存在,会自动创建,但不会创建文件夹
* 如果对应的文件夹目录不存在,就报错
*
* 构造方法:
* FIleOutputStream(String path): 向该文件中写出数据并覆盖原有内容
* FileOutputStream(String path,boolean append): 向该文件中写出数据,如果append为true就追加写出,如果为false就覆盖
* write(int): 写出一个数据
* write(byte[] b): 写出该数组中的所有内容
* write(byte[]b,int off,int len): 写出该数组中指定数据
* flush(): 刷缓存,不调用也可以,关闭流的时候会自动调用
* 辅助方法: getBytes():String 中的方法,用于获取字符串对应的字节数组
1.6.2 使用方式
try (
// FileOutputStream fos = new FileOutputStream("D:/a.txt");
FileOutputStream fos = new FileOutputStream("D:/a.txt", true);) {
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
fos.write('\n');
String str = "你好吗";
byte[] bytes = str.getBytes();
fos.write(bytes);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}
1.7 Writer
1.7.1 概述
上同
1.7.2 使用方式
try (FileWriter fw = new FileWriter("D:/a.txt");) {
// 写出字符串
fw.write("你好吗?\n");
char[] chars = { 'a', 'b', 'c', 'd' };
// 写出char数组
fw.write(chars);
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}
1.8 缓冲流
1.8.1 概述
1.8.2 BufferedInputStream
字节输入缓冲流
long startTime = System.currentTimeMillis();
try (FileInputStream fis = new FileInputStream("D:/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);) {
byte[] bytes = new byte[1024];
int temp = 0;
while ((temp = bis.read(bytes)) != -1) {
// System.out.println(new String(bytes,0,temp));
}
long endTime = System.currentTimeMillis();
System.out.println("读取完成,耗时 : " + (endTime - startTime));
// 字节流 : 120左右
} catch (Exception e) {
e.printStackTrace();
}
1.8.3 BufferedOutputStream
字节输出缓冲流
try (FileOutputStream fos = new FileOutputStream("D:/a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);) {
bos.write(97);
bos.write(98);
bos.write(99);
bos.write(100);
bos.write('\n');
String str = "你好吗";
byte[] bytes = str.getBytes();
bos.write(bytes);
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}
1.8.4 BufferedReader
字符输入缓冲流
try (FileReader fr = new FileReader("D:/a.txt");
BufferedReader br = new BufferedReader(fr);) {
String temp = null;
// 读取一行
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
1.8.5 BufferedWriter
字符输出缓冲流
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("D:/a.txt"));
bw.write("你好吗");
// 换行
bw.newLine();
bw.write("我很好");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}