拷贝文件到新路径的方法 java.io.File的API没有提供copy的接口,你可以自己实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用执行外部命令的方式好了,“copy” import java.io.*;public class CopyFile{ public static void copy(String from_name,String to_name) throws IOException { File from_file=new File(from_name); File to_file=new File(to_name); if(!from_file.exists()) abort("no such file:" + from_name); if(!from_file.isFile()) abort("a directory:" + from_name); if(!from_file.canRead()) abort("can't read:" + from_name); if(to_file.isDirectory()) to_file=new File(to_file,from_file.getName()); if(to_file.exists()) { if(!to_file.canWrite()) abort("Destination File is unwriteable:" + to_name); System.out.print("Overwrite existing file:" + to_file.getName() + "? (Y/N):"); System.out.flush(); BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); String res=in.readLine(); if(!res.equals("Y")&& !res.equals("y")) abort("not overwrite:" + to_name); }else{ String parent=to_file.getParent(); if(parent==null) parent=System.getProperty("user.dir"); File dir=new File(parent); if(!dir.exists()) abort("destination file is not exist:" + parent); if(dir.isFile()) abort("destination file is not a directory:" + parent); if(!dir.canWrite()) abort("can't write:" + parent); } FileInputStream from=null; FileOutputStream to=null; try{ from=new FileInputStream(from_file); to=new FileOutputStream(to_file); byte[] buffer=new byte[4096]; int bytes_read; while((bytes_read=from.read(buffer))!=-1) to.write(buffer,0,bytes_read); } finally{ if(from!=null) try{from.close();} catch(IOException e) { ;} if(to!=null) try{to.close();} catch(IOException e) {;} } } public static void abort(String msg) throws IOException { throw new IOException("filecopy:" + msg); } public static void main(String args[]) throws IOException { CopyFile cp=new CopyFile(); cp.copy(args[0],args[1]); }} 这是一个copy 文件的java程序试试看 关于泛型 https协议如何通过X509TrustManager接口实现自己创建的证书 JTEST检查时出现的错误 汉语乱码问题 谢谢大家解答,关于JAVA GUI设计的问题 该程序总是提示错误,能否帮助给纠正一下!(初学者) 小弟初学java问题多的要命~~~(请留oicq) java虚拟机的内存大小是否可变 java 资料 java高手,请问我应该怎样学好JAVA(只能给这么多了) 关于树的代码问题 帮忙翻译一句话!
{
public static void copy(String from_name,String to_name) throws IOException
{
File from_file=new File(from_name);
File to_file=new File(to_name);
if(!from_file.exists()) abort("no such file:" + from_name);
if(!from_file.isFile()) abort("a directory:" + from_name);
if(!from_file.canRead()) abort("can't read:" + from_name);
if(to_file.isDirectory()) to_file=new File(to_file,from_file.getName());
if(to_file.exists())
{
if(!to_file.canWrite())
abort("Destination File is unwriteable:" + to_name);
System.out.print("Overwrite existing file:" + to_file.getName() + "? (Y/N):");
System.out.flush();
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String res=in.readLine();
if(!res.equals("Y")&& !res.equals("y")) abort("not overwrite:" + to_name);
}else{
String parent=to_file.getParent();
if(parent==null) parent=System.getProperty("user.dir");
File dir=new File(parent);
if(!dir.exists()) abort("destination file is not exist:" + parent);
if(dir.isFile()) abort("destination file is not a directory:" + parent);
if(!dir.canWrite()) abort("can't write:" + parent);
}
FileInputStream from=null;
FileOutputStream to=null;
try{
from=new FileInputStream(from_file);
to=new FileOutputStream(to_file);
byte[] buffer=new byte[4096];
int bytes_read;
while((bytes_read=from.read(buffer))!=-1)
to.write(buffer,0,bytes_read);
}
finally{
if(from!=null) try{from.close();} catch(IOException e) { ;}
if(to!=null) try{to.close();} catch(IOException e) {;}
}
}
public static void abort(String msg) throws IOException
{
throw new IOException("filecopy:" + msg);
}
public static void main(String args[]) throws IOException
{
CopyFile cp=new CopyFile();
cp.copy(args[0],args[1]);
}
}
试试看