就是将一个文件夹复制到另一个指定的路径下,且该文件下还嵌套了文件夹和文件,就比如说将C盘下的Program File这个文件复制到一个指定的路径中,
当然该文件夹下的所有东西都要复制过去,望高手指教,谢谢~~
当然该文件夹下的所有东西都要复制过去,望高手指教,谢谢~~
解决方案 »
- 新手认识JavaFX
- 当一个方法抛出了异常,程序捕获,并处理了,程序接下了会怎样? 退出,还是继续运行
- 用java代码备份数据库,生成数据文件的问题
- 新手有疑问
- 在Panel中加添加一幅图片?
- 各位大侠什么是hardcode啊?
- 新手请教下actionPerformed抽象方法的返回值应该怎么做???(在线等,急~~~)
- 如何用JavaBean(纯Java语言写的)读写Cookie?,在线等,急
- 这么源代码不知道怎么测试它,args参数是干嘛用的
- 如何通过js改变onfocus的WdatePicker的属性
- 求类似大家来找茬游戏开发源码
- 多线程连接MySQL数据库,报 Communications link failure
(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
File a = new File(oldPath);
String[] file = a.list();
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
} if (temp.isFile()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath
+ "/" + (temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (temp.isDirectory()) {// 如果是子文件夹
copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace(); } }
String cmdstr="touch ./ri.txt";//自己在执行的命令
try {
//请参考DOS的cmd 与 Unix的sh
Process p=r.exec(cmdstr);
p.waitFor();//等待指令执行完毕
System.out.println("命令执行完毕");
} catch (Exception e) {
e.printStackTrace();
}
/*
1,建立目的目录。
2,遍历源目录。
3,遍历过程中,创建文件或者文件夹。
原理:其实就是改变了源文件或者目录的目录头。
*/
class CopyDir
{
private File sDir,dDir,newDir;
CopyDir(String s,String d)
{
this(new File(s),new File(d));
}
CopyDir(File sDir,File dDir)//c:\\Test d:\\abc
{
this.sDir = sDir;
this.dDir = dDir;
}
public void copyDir()throws IOException
{
//是创建目的目录。也就是创建要拷贝的源文件夹。Test
//获取源文件夹名称。
String name = sDir.getName();
//通过该名称在目的目录创建该文件夹,为了存放源文件夹中的文件或者文件夹。
//将目的目录和源文件夹名称,封装成File对象。
newDir = new File(dDir,name);
//调用该对象的mkdir方法。在目的目录创建该文件夹。d:\\abc\\Test
newDir.mkdir();// //遍历源文件夹。
listAll(sDir);
}
/*
将遍历目录封装成方法。
在遍历过程中,遇到文件创建文件。
遇到目录创建目录。
*/
private void listAll(File dir)throws IOException
{
File[] files = dir.listFiles();
for(int x=0; x<files.length; x++)
{
if(files[x].isDirectory())
{
createDir(files[x]);//调用创建目录的方法。
listAll(files[x]);//在继续进行递归。进入子级目录。
}
else
{
createFile(files[x]);//调用创建文件的方法。
}
}
}
/*
copy目录。通过源目录在目的目录创建新目录。
*/
private void createDir(File dir)
{
File d = replaceFile(dir);
d.mkdir();
}
/*
copy文件。
*/
private void createFile(File file)throws IOException
{
File newFile = replaceFile(file);
//copy文件是一个数据数据传输的过程。需要通过流来完成。
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(newFile);
byte[] buf = new byte[1024*2];
int num = 0;
while((num=fis.read(buf))!=-1)
{
fos.write(buf,0,num);
}
fos.close();
fis.close();
}
/*
替换路径。
*/
private File replaceFile(File f)
{
//原理是:将源目录的父目录(C:\\Tset),替换成目的父目录。(d:\\abc\\Test)
String path = f.getAbsolutePath();//获取源文件或者文件夹的决定路径。
//将源文件或者文件夹的绝对路径替换成目的路径。
String newPath = path.replace(sDir.getAbsolutePath(),newDir.getAbsolutePath());
//将新的目的路径封装成File对象
File newFile = new File(newPath);
return newFile;
}
}//主函数调用执行
import java.io.*;
class CopyDirDemo
{
public static void main(String[] args)throws IOException
{
//File s = new File("c:\\Test");
//File d = new File("d:\\abc");
new CopyDir("c:\\Test","d:\\abc").copyDir();
//System.out.println("Hello World!");
}
}