IO入门知识
什么是IO流:
IO Stream(input output Steam):输入输出流
本质指的就是计算机中数据的流入和流出:
从磁盘上将数据读入内存
从内存中将数据写入到硬盘
也就是说,从狭义上来说,我们一般IO流指的就是磁盘和内存之间的数据流动
但是从广义来说,不同电脑之间的数据流动,也是一种IO流,网络通信也是IO流(socket)
在本地进程间的数据流动,就是狭义上的IO流
在远程进程间的数据流动,也就是一种IO流(Socket)
java的IO流:
将底层的open函数进行了大量的封装,提供了大量好用的、符合各种场景的IO对象供大家使用
java.io包
InputStream类:字节输入流
OutputStream类:字节输出流
IO流的分类:
|-- 数据的流动方向
|-- 输入流 InputStream、Reader
|-- 输出流 OutputStream、Writer、Printer
|-- 根据的数据的类型
|-- 字节流 XxxxStream
|-- 字符类 Writer、Printer、Reader
|-- 根据作用
|-- 节点流
|-- 装饰流(过滤流)
|-- 转换流
转换字节流和字符流
字节流的基本使用
第一种:
@Test
void testio01() throws IOException {
// 创建输入流 -- 字节输入流
InputStream is = new FileInputStream(new File("C:\\Users\\liujianhong\\Desktop\\最近工作\\java se\\第1天笔记.txt"));
// 创建一个字节数组
byte[] buf = new byte[1024];
// read方法,会将文件中的数据读入一个字节数组中,返回值是读入数据的下标,-1下标表示文件末尾
while (is.read(buf) != -1) {
System.out.write(buf);
}
// IO流,打开之后,必须关闭!!!
is.close();
}
第二种:该种使用方法会有最后一次问题
@Test
void testio02() {
FileInputStream fis = null;
try {
fis = new FileInputStream("I:\\java\\java_se\\2021\\day19\\src\\com\\openlab\\day19\\collections\\User.java");
// 使用字节数组,作为缓存,进行数据读取,虽然效率是高,但是存在着一个问题,最后一次问题!!!
byte[] buf = new byte[1024];
while(fis.read(buf) != -1) {
System.out.write(buf);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第三种:解决了最后一次问题
@Test
void testio03() {
FileInputStream fis = null;
try {
fis = new FileInputStream("I:\\java\\java_se\\2021\\day19\\src\\com\\openlab\\day19\\collections\\User.java");
// 使用字节数组,作为缓存,进行数据读取,虽然效率是高,但是存在着一个问题,最后一次问题!!!
byte[] buf = new byte[1024];
// 定义一个变量,用来记得读取的位数,这样就解决了最后一次问题
int len = -1;
while((len = fis.read(buf)) != -1) {
System.out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用字节流完成文件的拷贝
@Test
void test04() {
// 文件拷贝
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("I:\\java\\java_se\\2021\\day19\\src\\com\\openlab\\day19\\collections\\User.java");
os = new FileOutputStream(new File("c:\\User.java"));
byte[] buf = new byte[1024];
int len = -1;
while((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
System.out.println("文件拷贝成功!!!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
装饰流(过滤流)的使用
装饰流是一种装饰者设计模式体现:
gof 23设计模式:
装饰者设计模式
以下代码包含了装饰流的使用,以及使用装饰流去拷贝文件的方法:
package com.openlab.day20.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.jupiter.api.Test;
public class TestFilterIO {
@Test
void testFilter01() {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream("c:/User.java"));
byte[] buf = new byte[1024];
int len = -1;
while((len = bis.read(buf)) != -1) {
System.out.write(buf, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
void test02() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// bis = new BufferedInputStream(new FileInputStream("I:\\上课视频\\java\\java se\\2021年暑假班\\01.计算机的基本概念.mp4"));
bis = new BufferedInputStream(new FileInputStream("G:\\pictures\\b.jpg"), 8000);
bos = new BufferedOutputStream(new FileOutputStream(new File("c:/a.jpg")), 8000);
// 如果使用缓存流,是可以直接读取的
// bos.write(bis.available());
byte[] buf = new byte[1024];
int len = -1;
while((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
// 刷新缓冲区
// bos.flush();
System.out.println("文件拷贝完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null)
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}