用java或者jsp,如何将文件从一个目录移动到另外一个目录的方法?

Lihaitao312 2004-01-09 08:56:09
我查找了关于file中的部分方法,没有发现如何移动的方法,请指教!
...全文
76 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
telent 2004-01-09
  • 打赏
  • 举报
回复
Yoinn(孤标傲视皆谁引,一样开花为底迟)
well do!
lovingoliver 2004-01-09
  • 打赏
  • 举报
回复
already note it!
Yoinn 2004-01-09
  • 打赏
  • 举报
回复
呵呵!!
Yoinn 2004-01-09
  • 打赏
  • 举报
回复
public boolean Move(String from,String to){
File ffrom = new File(from);
File fto = new File(to);
if(ffrom==null) return false;
try{
if(fto!=null&&!fto.exists()){
return ffrom.renameTo(fto);
}
}catch(Exception e){}
return false;
}
lovingoliver 2004-01-09
  • 打赏
  • 举报
回复
A)把源文件讀出來,再將其寫入新文件里,然后刪除源文件,copy方法如下:
String sourcefile=null,destfile=null;
byte[] b=new byte[1024*5];
int len;
FileInputStream input=new FileInputStream(sourcefile);
FileOutputStream output=new FileOutputStream(destfile);
while ((len=input.read(b))!=-1)
{
output.write(b,0,len);
}
out.flush();
output.close();
input.close();
B)利用Runtime.getRuntime().exec(doscommand)來執行外部命令,
實現文件的轉移。
79cy 2004-01-09
  • 打赏
  • 举报
回复
COPY原目录文件->删除原目录文件->复制到新目录下文件。

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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