一、大体结构
IO流:见名知意就是 输入、输出流
一、 从处理数据来看
- 处理数据的话为字节流,加一个认知吧
字节流:Stream结束的都是字节流
- 因为我们经常用的中文,英文各种语言就有了字符流
字符流:是Reader和Witer的子类(下面有贴图)
- 字节流和字符流之间的转换就引入了转换流
转换流:InputStreamReader/OutputStreamWriter
(即把我们的语言转换成电脑的 字节;同理电脑的输出为 字符语言)
先贴两张知识体系图吧
详细的可以去看API,注意 继承体系, 构造器,
方法(方法名,形参,返回值)
二、从流向来看
根据流向我们分为输入流和输出流
以程序为中心
- 程序—》字节 输入流
- 字节—》程序 输出流
三、从操作对象看
操作对象是文件、字节数组为节点流
操作在节点之上的话为处理流,也叫装饰流,提升了节点的性能,方便更好的操作
二 、 拷贝(挺重要的)
//操作io的大体思路
- 创建源
- 创建流
- 具体操作
- 释放资源
//贴一段代码吧
public class Copy {
public static void main(String[] args) {
copy("src/io/Copy.java","copy.txt");
}
/**
* 文件的拷贝
* 思考: 利用递归 制作文件夹的拷贝
* @param srcPath
* @param destPath
*/
public static void copy(String srcPath,String destPath) {
//1、创建源
File src = new File(srcPath); //源头
File dest = new File(destPath);//目的地
//2、选择流
InputStream is =null;
OutputStream os =null;
try {
is =new FileInputStream(src);
os = new FileOutputStream(dest);
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len); //分段写出
}
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
//4、释放资源 分别关闭 先打开的后关闭
try {
if (null != os) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
`