黑马程序员技术交流社区
标题:
关于拷贝文件夹的问题
[打印本页]
作者:
梁健
时间:
2011-12-31 23:41
标题:
关于拷贝文件夹的问题
就是我写了一程序,从C盘拷贝一个文件夹的所有内容到D盘,如果我把这个程序拿到别的机器上运行,那我需要把这个文件夹也附带进去吗?怎么附带呢?还是。。。。求高手。
作者:
刘忠德
时间:
2012-1-1 00:56
你写的程序已经显式地写明哪个文件夹从哪拷到哪了,拿到别的机器,你很显然不能确定别的机器一定有你要拷的资源文件和目标地址,所以~~
这个需要在运行的机器上重新配置 源路径和目标路径~
作者:
梁健
时间:
2012-1-1 08:51
哦,晓得了,谢谢
作者:
付旭
时间:
2012-1-1 10:17
都不晓得 你在问什么
作者:
王冀0127
时间:
2012-1-2 13:36
其实你可以写几段从键盘输入的代码,然后再在代码中加入判断文件夹是否存在的判断语句,这样你的程序就健壮多了!这样你放到别人的电脑上运行应该就没有别的大问题应该
作者:
左华清
时间:
2012-1-2 23:20
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public boolean copy(String file1,String file2) {
File in=new File(file1);
File out=new File(file2);
if(!in.exists()){
System.out.println(in.getAbsolutePath()+"源文件路径错误!!!");
return false;
}
else {
System.out.println("源文件路径"+in.getAbsolutePath());
System.out.println("目标路径"+out.getAbsolutePath());
}
if(!out.exists())
out.mkdirs();
File[] file=in.listFiles();
FileInputStream fin=null;
FileOutputStream fout=null;
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
try {
fin=new FileInputStream(file[i]);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("in.name="+file[i].getName());
try {
fout=new FileOutputStream(new File(file2+"/"+file[i].getName()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(file2);
int c;
byte[] b=new byte[1024*5];
try {
while((c=fin.read(b))!=-1){
fout.write(b, 0, c);
System.out.println("复制文件中!");
}
fin.close();
fout.flush();
fout.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else copy(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}
return false;
}
public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
copyFile.copy("c:\\chianpayInterface", "e:\\copytest");
}
}
换下你的文件路径就ol
作者:
黑马张伟
时间:
2012-1-3 08:13
你现在写的代码是针对你电脑上的路径和文件,你去别的机子上运行人家的机子上不一定有你的路径和文件,而且你拷过去也没有必要,还不如去那个机子上,重新选择一个路径和文件夹,这样还比较简单点,你是为了实现拷贝文件的方法,不是为了拷贝你电脑上的文件,所以我认为没那个必要
作者:
胡军喜
时间:
2012-3-1 15:43
本帖最后由 胡军喜 于 2012-3-1 15:46 编辑
自己写的例子,经测试,可以拷贝:
拷贝文件的方法单独抽取出来,这样既可以拷贝文件,又可以拷贝文件夹,其中拷贝文件夹调用了拷贝文件的方法。
*****************************************代码开始*****************************************
package cn.itcast.entrancetest.problem08;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
/** 编写程序,拷贝一个带内容的文件夹。 例如:将c:\program files\java 文件夹拷贝到d盘根目录 **/
public static void main(String[] args) throws Exception {
// 测试,把D:\Program Files\Java拷贝到E盘根目录
System.out.println("文件夹拷贝结果:"
+ copyDir("D:\\Program Files\\Java", "E:\\"));
}
// 拷贝目录
public static boolean copyDir(String originPath, String destPath)
throws Exception {
File originDir = new File(originPath);
File destDir = new File(destPath);
if (!originDir.exists()) {
System.out.println("源目录不存在");
return false;
}
if (!destDir.exists()) {
System.out.println("目标目录不存在,正在创建目标目录……");
destDir.mkdir();
System.out.println("目标目录创建成功!");
}
File[] files = originDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
copyDir(originPath + "\\" + file.getName(), destPath + "\\"
+ file.getName());
} else {
copyFile(originPath + "\\" + file.getName(), destPath + "\\"
+ file.getName());
}
}
return true;
}
// 拷贝文件
public static void copyFile(String in, String out) {
FileOutputStream fw = null;
FileInputStream fr = null;
try {
fw = new FileOutputStream(out);
fr = new FileInputStream(in);
byte[] bt = new byte[1024];
int len = 0;
while ((len = fr.read(bt)) != -1) {
fw.write(bt, 0, len);
}
} catch (IOException e) {
throw new RuntimeException("读写失败!");
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
}
}
*****************************************代码结束*****************************************
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2