在线等:用File类copy文件的时候怎么使源文件保留?

千山独行 2004-08-11 02:35:47
我想实现这样的功能:在某个目录下有一个文件,想把它copy到其他目录下,我写了一些代码,怎么发现是把源文件剪切到其他目录下了,怎么在copy的同时,源文件不动呢?
...全文
123 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomcatjava 2004-08-11
  • 打赏
  • 举报
回复
import java.io.*;

public class FileCopy
{
private File src = null;
private File des = null;

public FileCopy( File src,File des ) {
this.src = src;
this.des = des;
}

public boolean copy() {
try{
BufferedReader in = new BufferedReader(
new FileReader(src) );
BufferedWriter out = new BufferedWriter(
new FileWriter(des) );
String line = null;
StringBuffer t = new StringBuffer("");
while( (line = in.readLine()) != null ) {
t.append( line + "\r\n" );
}
in.close();
out.write( t.toString() );
out.flush();
out.close();
}
catch( IOException e ) {
System.out.println( "Wrong" );
return false;
}

return true;
}

public static void main( String args[] ) {
File src = new File( ".","FileCopy.java" );
File des = new File( ".","Copy.java" );
FileCopy copy = new FileCopy( src,des );
copy.copy();
}
}
shine333 2004-08-11
  • 打赏
  • 举报
回复
从FileInputStream里面读,往FileOutputStream里面写不就可以了
shine333 2004-08-11
  • 打赏
  • 举报
回复
用一个FileInputStream和一个FileOutputStream配对使用

62,623

社区成员

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

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