复制文件
封装后的复制文件方法
- 接收参数为两个File对象,代表输入和输出文件,并声明抛出IOException异常
public static void CopyFile(File src, File dest) throws IOException;
- 判断是否为文件夹,如果是文件夹则抛出异常
if (src.isDirectory()) {
throw new IOException("这是一个文件夹");
}
- 建立输入输出流
InputStream iStream = new FileInputStream(src);
OutputStream oStream = new FileOutputStream(dest);
- 创建字节数组为复制文件做准备,建立len整型变量记录长度
byte[] flush = new byte[1024];
int len = 0;
- 在未到达文件尾之前,读取文件并写入目标
while (-1 != (len = iStream.read(flush))) {
oStream.write(flush, 0, len);
}
- 强制刷新输出流
out.flush();
- 关闭输入输出流
oStream.close();
iStream.close();
完整方法:
public static void CopyFile(File src, File dest) throws IOException {
if (src.isDirectory()) {
throw new IOException("这是一个文件夹");
}
InputStream iStream = new FileInputStream(src);
OutputStream oStream = new FileOutputStream(dest);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = iStream.read(flush))) {
oStream.write(flush, 0, len);
}
oStream.flush();
oStream.close();
iStream.close();
}
重载方法,使其可以接受String类型
public static void CopyFile(String srcPath, String destPath) throws IOException {
CopyFile(new File(srcPath), new File(destPath));
}
复制文件夹
封装后的复制文件方法
- 接收参数为两个File对象,代表输入和输出文件,并声明抛出IOException异常
public static void Copydirs(File src, File dest) throws IOException
- 判断是否为文件夹,如果是文件夹则在目标文件夹下建立源文件夹,调用复制文件夹
public static void Copydirs(File src, File dest) throws IOException {
if (src.isDirectory()) {
dest = new File(dest, src.getName());
}
CopyDir(src, dest);
}
- 判断是否为文件,如果是文件就直接复制,如果不是就建立文件夹然后再复制
private static void CopyDir(File src, File dest) throws IOException {
if (src.isFile())
CopyFile(src, dest);
else if (src.isDirectory()) {
dest.mkdirs();
for (File sub : src.listFiles()) {
CopyDir(sub, new File(dest, sub.getName()));
}
}
}
重载方法,使其可以接受String类型
public static void Copydirs(String srcPath, String destPath) throws IOException {
Copydirs(new File(srcPath), new File(destPath));
}
完整代码
package cn.hxh.io.stream;
import java.io.*;
public class CopyUtil {
public static void main(String[] args) {
}
public static void CopyFile(String srcPath, String destPath) throws IOException {
CopyFile(new File(srcPath), new File(destPath));
}
public static void CopyFile(File src, File dest) throws IOException {
if (src.isDirectory()) {
throw new IOException("这是一个文件夹");
}
InputStream iStream = new FileInputStream(src);
OutputStream oStream = new FileOutputStream(dest);
byte[] flush = new byte[1024];
int len = 0;
while (-1 != (len = iStream.read(flush))) {
oStream.write(flush, 0, len);
}
oStream.flush();
oStream.close();
iStream.close();
}
public static void Copydirs(String srcPath, String destPath) throws IOException {
Copydirs(new File(srcPath), new File(destPath));
}
public static void Copydirs(File src, File dest) throws IOException {
if (src.isDirectory()) {
dest = new File(dest, src.getName());
}
CopyDir(src, dest);
}
private static void CopyDir(File src, File dest) throws IOException {
if (src.isFile())
CopyFile(src, dest);
else if (src.isDirectory()) {
dest.mkdirs();
for (File sub : src.listFiles()) {
CopyDir(sub, new File(dest, sub.getName()));
}
}
}
}