Java实现Copy
2007-06-21 08:54
309 查看
假定源文件夹为D:/网络管理,目标文件夹为D:/网络管理2,将目录及子目录下的文件做拷贝
package test;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Copys {
public static void main(String args[]) {
File file = new File("D://网络管理");
File tofile = new File("D://网络管理2");
tofile.mkdirs();//建立目标文件夹
//指定源文件夹和目标文件夹参数,调用toCopyf方法
toCopyf(file, tofile);
}
/**
* 持贝指定的目录下所有文件及子目录到目标文件夹
* */
public static void toCopyf(File file, File tofile) {
//获取源目录下一级所有目录文件
File[] files = file.listFiles();
//逐个判断,创建目录,执行递归调用
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
File copyPath = new File(tofile.getAbsolutePath() + "//" +
files[i].getName());
copyPath.mkdir();
toCopyf(files[i],copyPath);
} else { //如果file为文件,读取字节流写入目标文件;
try {
FileInputStream fiStream = new FileInputStream(files[i]);
BufferedInputStream biStream = new BufferedInputStream(
fiStream);
File copyFile = new File(tofile.getAbsolutePath()
+ "//" + files[i].getName());
copyFile.createNewFile();
FileOutputStream foStream = new FileOutputStream(copyFile);
BufferedOutputStream boStream = new BufferedOutputStream(
foStream);
int j;
while ((j = biStream.read()) != -1) {
boStream.write(j);
}
/*关闭流*/
biStream.close();
boStream.close();
fiStream.close();
foStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
package test;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class Copys {
public static void main(String args[]) {
File file = new File("D://网络管理");
File tofile = new File("D://网络管理2");
tofile.mkdirs();//建立目标文件夹
//指定源文件夹和目标文件夹参数,调用toCopyf方法
toCopyf(file, tofile);
}
/**
* 持贝指定的目录下所有文件及子目录到目标文件夹
* */
public static void toCopyf(File file, File tofile) {
//获取源目录下一级所有目录文件
File[] files = file.listFiles();
//逐个判断,创建目录,执行递归调用
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
File copyPath = new File(tofile.getAbsolutePath() + "//" +
files[i].getName());
copyPath.mkdir();
toCopyf(files[i],copyPath);
} else { //如果file为文件,读取字节流写入目标文件;
try {
FileInputStream fiStream = new FileInputStream(files[i]);
BufferedInputStream biStream = new BufferedInputStream(
fiStream);
File copyFile = new File(tofile.getAbsolutePath()
+ "//" + files[i].getName());
copyFile.createNewFile();
FileOutputStream foStream = new FileOutputStream(copyFile);
BufferedOutputStream boStream = new BufferedOutputStream(
foStream);
int j;
while ((j = biStream.read()) != -1) {
boStream.write(j);
}
/*关闭流*/
biStream.close();
boStream.close();
fiStream.close();
foStream.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
}
相关文章推荐
- 如何在Java应用程序中实现copy图像功能。
- Java—使用System.arraycopy()实现数组之间的复制
- protobuf实现c++与java之间的数据传递,beancopy数据到前台
- [z]protobuf实现c++与java之间的数据传递,beancopy数据到前台
- 【LeetCode-面试算法经典-Java实现】【138-Copy List with Random Pointer(拷贝有随机指针的单链表)】
- java实现动态智能数组,将旧数组copy到新数组
- 用JAVA实现Undo、Redo,Copy、Paste、Cut
- JAVA反射实现基本的copyProperty,getProperty,isEmpty方法
- PostgreSQL:Java使用CopyManager实现客户端文件COPY导入 .
- PostgreSQL:Java使用CopyManager实现客户端文件COPY导入
- Java 实现文件上传、下载、打包、文件copy、文件夹copy。
- JAVA中写时复制(Copy-On-Write)Map实现
- Java实现DOS中的Copy命令
- leetcode:Copy List with Random Pointer 细致分析,以及代码实现(JAVA版本)
- PostgreSQL:Java使用CopyManager实现客户端文件COPY导入
- 10059---java反射实现bean的copy
- Java实现DOS中的Copy命令
- java如何快速实现深copy
- 利用java反射实现任意数组copy,reflect.Array
- Java中实现复制文件或文件夹——CopyUtil.java