1.1 缓冲流
1.缓冲流涉及到的类:
- BufferedInputStream
- BufferedOutputStream
- BufferedReader
- BufferedWriter
2.作用:
作用:提高流的读取、写入的速度
提高读写速度的原因:内部提供了一个缓冲区。默认情况下是8kb
1.1.1 使用BufferedInputStream和BufferedOutputStream处理非文本文件
例:复制图片xx.jpg为zz.jpg
@Test
public void test01(){
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//1.创建File类的对象,指明读入和写出的文件
File file = new File("xx.jpg");
File descFile = new File("zz.jpg");
//2.1创建输入流和输出流的对象
fis = new FileInputStream(file);
fos = new FileOutputStream(descFile);
//2.2造缓冲流
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.数据的读入和写出操作
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//4.资源关闭
//要求:先关闭外层的流,再关闭内层的流
if (bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略
// fos.close();
// fis.close();
}
}
}
注:重要的点:资源关闭时,先关闭外层的流,再关闭内层的流
关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略
1.1.2 使用BufferedReader和BufferedWriter:处理文本文件
例:将hello1.txt文件复制为hello3.txt
@Test
public void test02(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
//创建文件和相应的流
br = new BufferedReader(new FileReader(new File("hello1.txt")));
bw = new BufferedWriter(new FileWriter(new File("hello3.txt")));
//读写操作
char[] chars = new char[5];
int len;
while ((len = br.read(chars)) != -1){
bw.write(chars,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bw != null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.2 转换流
1.2.1 转换流涉及到的类:属于字符流
-
InputStreamReader:将一个字节的输入流转换为字符的输入流
解码:字节、字节数组 —>字符数组、字符串 -
OutputStreamWriter:将一个字符的输出流转换为字节的输出流
编码:字符数组、字符串 —> 字节、字节数组
说明:编码决定了解码的方式
作用:提供字节流与字符流之间的转换
图示:
例:综合使用InputStreamReader和OutputStreamWriter
@Test
public void test02(){
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
//1.造文件、造流
File file1 = new File("hello1.txt");
File file2 = new File("hello_gbk.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fir = new FileOutputStream(file2);
isr = new InputStreamReader(fis,"utf-8");
osw = new OutputStreamWriter(fir,"gbk");
//2.读写过程
char[] chars = new char[5];
int len;
while ((len = isr.read(chars)) != -1){
osw.write(chars,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//3.关闭资源
if (osw != null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.3 数据流DataInputStream和DataOutputStream
作用:
用于读取或写出基本数据类型的变量或字符串
例1:将内存中的字符串、基本数据类型的变量写出到文件中。
@Test
public void test01(){
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("hhh.txt"));
dos.writeInt(123);
dos.flush();
dos.writeUTF("哈哈");
dos.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (dos != null){
try {
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
例2:将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
注意点:读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致!
@Test
public void test02() {
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("hhh.txt"));
int id = dis.readInt();
String name = dis.readUTF();
System.out.println("name = " + name);
System.out.println("id = " + id);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (dis != null){
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
文章参考:
b站尚硅谷宋红康老师Java教学视频;