IO流——字节流
1、 概念
流:即流动,从一端移动到另一端,源头和目的地
程序 与 文件 | 数组 | 网络连接 | 数据库
以程序为中心
2、 IO流分类
(1).按数据的流向分:输入流 输出流
(2).按处理的数据分:字节流 字符流
字节流:二进制,可以处理一切文件,包括纯文本、doc、音频、视频等等
字符流:文本文件,只能处理纯文本
(3).按功能来分:节点流 处理流
节点流:包裹源头
处理流:增强功能,提高性能
3、字节流与字符流 与 文件
(1).字节流
输入流:InputStream FileInputStream()
read(byte[] b)
read(byte[] b, int off, int len)
close()
输出流:OutputStream FielOutputStream()
write(byte[] b)
write(byte[] b, int off, int len)
flush()
close()
(2).字符流
输入流:Reader FileReader()
read(char[] cbuf)
read(char[] cbuf, int off,int len)
close()
输出流:Writer FileWriter()
write(char[] cbuf)
write(char[] cbuf, int off,int len)
flush()
close()
write(
String str, int off, int len)
4、操作
(1).建立联系
(2).选择流
(3).具体操作 (数组大小、read | write)
(4).释放资源
5、字节流读取文件
//1、建立联系,File对象
String path = "D:/IOtest/FileMethodTest.java";
File file = new File(path);
//2、选择流
InputStream inputStream = null; //提升作用域
try {
inputStream = new FileInputStream(file);
//3、操作不断读取
byte[] car = new byte[10]; //缓冲数组
int len = 0; //接收实际读取大小
//循环读取
while (-1 != (len = inputStream.read(car))) {
//这里进行输出处理,字节数组转换成字符串
String info = new String(car,0,len);
System.out.println(info);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件读取失败");
} finally {
//4、释放资源
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭文件输入流失败");
}
}
}
6、字节流写文件
//1、建立联系
String path = "D:/IOtest/writeFileTest.txt";
File file = new File(path);
//2、选择流
OutputStream outputStream = null;
try {
//以追加的形式写出文件,必须设为true,否则就是覆盖
outputStream = new FileOutputStream(file,true);
//3、操作
String str = "Hello World!!! \r\n";
//将字符串转为字节数组
byte[] data = str.getBytes();
outputStream.write(data,0,data.length);
//强制刷新出去
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件写出失败");
} finally {
//4、释放资源
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("关闭文件输出流失败");
}
}
}
7、 文件的拷贝(以程序为中心)
(1)、建立联系 File对象 源头 目的地
(2)、选择流
文件输入流 InputStream FileInputStream()
文件输出流 OutputStream FileOutputStream()
(3)、操作:拷贝
byte[ ]flush = new byte[1024];
int len= 0;
while(-1!= (len = 输入流.read(flush))) {
输出流.write(flush, 0, len);
}
输出流.flush();
(4)、释放资源 关闭两个流
//1、建立联系 源(存在且为文件) + 目的地(文件可以不存在
File src = new File(srcPath);
File dest = new File(destPath);
if (!src.isFile()) {
System.out.println("不是文件");
throw new IOException("只能拷贝文件");
}
//2、选择流
InputStream inputStream = new FileInputStream(src);
OutputStream outputStream = new FileOutputStream(dest);
//3、文件拷贝 循环+读取+写出
byte[] flush = new byte[1024];
int len = 0;
//读取
while (-1 != (len = inputStream.read(flush))) {
//写出
outputStream.write(flush,0,len);
}
outputStream.flush(); //强制刷出
//4、释放资源 关闭流
outputStream.close();
inputStream.close();
2. 文件夹的拷贝
(1)、递归查找子孙级文件 | 文件夹
(2)、如果是文件,进行复制操作即可(IO流复制) copyFile()
(3)、如果是文件夹,进行创建操作即可 mkdirs()
import java.io.File;
import java.io.IOException;
public class CopyDir {
public static void main(String[] args) {
String srcPath = "D:/IOtest/picture/1.jpg";
String destPath = "D:/IOtest/picture/copyTest";
copyDir(srcPath,destPath);
}
public static void copyDir(String srcPath,String destPath) {
File src = new File(srcPath);
File dest = new File(destPath);
copyDir(src,dest);
}
/**
* 拷贝文件夹
* @param src
* @param dest
*/
public static void copyDir(File src,File dest) {
if (src.isDirectory()) { //是文件夹
dest = new File(dest,src.getName());
}
copyDirDetail(src,dest);
}
/**
* 拷贝文件夹细节
* @param src
* @param dest
*/
public static void copyDirDetail(File src,File dest) {
if (src.isFile()) {
try {
FileUtil.copyFile(src,dest);
} catch (IOException e) {
e.printStackTrace();
}
} else if (src.isDirectory()) {
//确保目标文件夹的存在
dest.mkdirs();
//获取下一级目录|文件
for (File sub : src.listFiles()) {
copyDirDetail(sub,new File(dest,sub.getName()));
}
}
}
}
copyFile(File src,File dest)方法
/**
* 文件的拷贝
* @param src 源文件
* @param dest 目标文件
* @throws IOException
*/
public static void copyFile(File src, File dest) throws IOException {
if (!src.isFile()) { //不是文件或为null
System.out.println("不是文件");
throw new IOException("只能拷贝文件");
}
if (dest.isDirectory()) { //dest为已经存在的文件夹,不能建立与文件夹同名的文件
System.out.println("不能建立与文件夹同名的文件");
throw new IOException("不能建立与文件夹同名的文件");
}
//2、选择流
InputStream inputStream = new FileInputStream(src);
OutputStream outputStream = new FileOutputStream(dest);
//3、文件拷贝 循环+读取+写出
byte[] flush = new byte[1024];
int len = 0;
//读取
while (-1 != (len = inputStream.read(flush))) {
//写出
outputStream.write(flush,0,len);
}
outputStream.flush(); //强制刷出
//4、释放资源 关闭流
outputStream.close();
inputStream.close();
}