Java复制文件很简单,我们可以直接通过io操作即可,但复制一个文件夹时就不同了。最主要的难点在于文件的结构。
一、实现思路:
1.遍历目标文件下的所有子文件和目录。
2.遍历所有子文件判断是目录还是文件,如果是目录的话先创建该目录,然后再递归进入该目录遍历,反之则复制文件。
3.再次判断目录下的文件类型,重复以上操作。
二、流程:
主函数:
主函数如以下代码所示,包含源文件路径与复制目标文件,通过调用copyMethod方法实现文件复制。
public class CopyTest {
public static void main(String[] args) {
//计划复制文件,
File screFile = new File("D:\\JAVA\\Yest");//填入复制文件路径例如:D:\JAVA\代码练习
//拷贝目标
File destFile = new File("D:\\flash");//填入复制目标文件路径例如:D:\flash
//调用拷贝方法传入目标源文件路径与复制文件路径
copyMethod(screFile, destFile);
}
copyMethod方法:(包含文件类型判断,文件复制)
首先,应该对传入的文件进行判断,如果文件是一个文件夹,则开始获取文件夹的子目录,并通过递归进行循环调用,直到目标为普通文件为止。
//获取源文件子目录
File[] files = screFile.listFiles();
for (File file : files) {
//获取所有文件的目录以及目录下的所有子文件的绝对路径
if (file.isDirectory()) {
//如果这个文件下还有文件。则创建子目录
String scrDir = file.getAbsolutePath();
String desDir = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath()+ "\\")
+ scrDir.substring(3);
File newFile = new File(desDir);
if (!newFile.exists()) {
newFile.mkdirs();
}
}
//调用递归
copyMethod(file, destFile);
}
当发现当前文件为普通文件时,则开始进行文件的写入,此时需要对用户输入的文件路径进行拼接,确保可以将源文件复制到目标文件夹下。
if (screFile.isFile()) {
//创建输入流
FileInputStream inputStream = null;
//创建输出流
FileOutputStream outputStream = null;
try {
//开始读取目标文件,将screFile传入并开始读取文件
inputStream = new FileInputStream(screFile);
/* 写入文件,由于拷贝文件与目标文件可能不在同一盘符下,所以这里需要进行字符串的截取,来获得指定的拷贝目标
name 变量中存放指定拷贝目标路径
由于可能指向空的文件夹,这需要对拷贝目标文件经行判断,如果拷贝文件结尾为一个文件夹名称,则在后面加入\\
* */
String name = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\")
+ screFile.getAbsolutePath().substring(3);
//传入输出文件路径
outputStream = new FileOutputStream(name);
//经行文件的输入与输出,
byte[] bytes = new byte[1024 * 1024];
//每次复制1m的内容
int readCount = 0;
while ((readCount = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, readCount);
}
//刷新流
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
经过以上程序,即实现文件的复制。
三、完整代码如下:
public class CopyTest {
public static void main(String[] args) {
//计划复制文件,
File screFile = new File("D:\\JAVA\\Yest");//填入复制文件路径例如:D:\JAVA\代码练习
//拷贝目标
File destFile = new File("D:\\flash");//填入复制目标文件路径例如:D:\flash
//调用拷贝方法传入目标源文件路径与复制文件路径
copyMethod(screFile, destFile);
}
/**
* 文件拷贝(目录)
*
* @param screFile 拷贝文件路径
* @param destFile 拷贝目标文件位置
*/
private static void copyMethod(File screFile, File destFile) {
//判断是否是一个文件,如果是一个文件,则结束方法
if (screFile.isFile()) {
//创建输入流
FileInputStream inputStream = null;
//创建输出流
FileOutputStream outputStream = null;
try {
//开始读取目标文件,将screFile传入并开始读取文件
inputStream = new FileInputStream(screFile);
/* 写入文件,由于拷贝文件与目标文件可能不在同一盘符下,所以这里需要进行字符串的截取,来获得指定的拷贝目标
name 变量中存放指定拷贝目标路径
由于可能指向空的文件夹,这需要对拷贝目标文件经行判断,如果拷贝文件结尾为一个文件夹名称,则在后面加入\\
* */
String name = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\")
+ screFile.getAbsolutePath().substring(3);
//传入输出文件路径
outputStream = new FileOutputStream(name);
//经行文件的输入与输出,
byte[] bytes = new byte[1024 * 1024];
//每次复制1m的内容
int readCount = 0;
while ((readCount = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, readCount);
}
//刷新流
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return;
}
//获取源文件子目录
File[] files = screFile.listFiles();
for (File file : files) {
//获取所有文件的目录以及目录下的所有子文件的绝对路径
if (file.isDirectory()) {
//如果这个文件下还有文件。则创建子目录
String scrDir = file.getAbsolutePath();
String desDir = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath()+ "\\")
+ scrDir.substring(3);
File newFile = new File(desDir);
if (!newFile.exists()) {
newFile.mkdirs();
}
}
//调用递归
copyMethod(file, destFile);
}
}
}