java菜鸟有问题想请教各位大师、、

liangzaizjl 2011-09-01 05:52:26
在java中怎么将一个文件复制到另外一个目录上去。。我要实现的代码
...全文
76 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
千与 2011-09-01
  • 打赏
  • 举报
回复
改下代码就ok了,只支持复制单个文件:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class CopyFile {
public void copy(String file1, String file2) throws Exception {

File in = new File(file1);
File out = new File(file2);
if (!in.exists()) {
throw new Exception("源文件不存在:" + in.getAbsolutePath());
}
if (!out.exists()) {
out.mkdirs();
}
FileInputStream fin = null;
FileOutputStream fout = null;
if(in.isFile()) {
fin = new FileInputStream(in);
fout = new FileOutputStream(new File(out + "/" + in.getName()));
int c;
byte[] b = new byte[1024 * 5];
while ((c = fin.read(b)) != -1) {

fout.write(b, 0, c);
}
fin.close();
fout.close();
} else {
System.out.println(in.getName() + "是文件,不能指定目录。");
}
}

public static void main(String[] args) throws Exception {
CopyFile copyFile = new CopyFile();
copyFile.copy("E:\\index\\segments.gen", "E:\\");
}
}
liangzaizjl 2011-09-01
  • 打赏
  • 举报
回复
不能直接复制一个指定的文件吗?一定要放在一个目录下才可以复制,
copyFile.copy("D:\\新建文件夹", "f:/"); 我把文件放在这个目录下就行。这是为什么??
千与 2011-09-01
  • 打赏
  • 举报
回复
copyFile.copy("d:/123.jpg", "f:/");
第一个参数需要制定路径才能复制,复制该路径下的所有文件到f:\\
liangzaizjl 2011-09-01
  • 打赏
  • 举报
回复
public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
copyFile.copy("d:/123.jpg", "f:/");
}


我这样写了,报了下面这个错,没有拷贝过去,为什么??是不是路径有问题??

源文件路径d:\123.jpg
目标路径f:\
Exception in thread "main" java.lang.NullPointerException
at test2.CopyFile.copy(CopyFile.java:26)
at test2.CopyFile.main(CopyFile.java:67)
ChangeZ_ 2011-09-01
  • 打赏
  • 举报
回复
谢谢二楼。
昆卡卡 2011-09-01
  • 打赏
  • 举报
回复
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();
}
<-------------------------------注释掉
// return true;
}
else copy(file1+"/"+file[i].getName(),file2+"/"+file[i].getName());
}

return false;


}

public static void main(String[] args) {
CopyFile copyFile = new CopyFile();
copyFile.copy("E:\\study\\opngl", "E:\\opengl");
}
}
楼主看下

51,409

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧