大约是这样,没有测试
效率也不太好import java.io.*;try{
FileInputStream fis = new FileInputStream("f:\\temp\\name.txt");
FileOutPutStream fos = new FileOutputStream("d:\\temp\\name.txt");
while((int c = fis.read())!=-1){
fos.write(c);
}
fis.close();
fos.close();
}catch(IOException e){
System.err.println(e.getMessage());
}
效率也不太好import java.io.*;try{
FileInputStream fis = new FileInputStream("f:\\temp\\name.txt");
FileOutPutStream fos = new FileOutputStream("d:\\temp\\name.txt");
while((int c = fis.read())!=-1){
fos.write(c);
}
fis.close();
fos.close();
}catch(IOException e){
System.err.println(e.getMessage());
}
public static void main(String[] args){
try{
if(args.length == 2){
new Copy().copy2(args[0], args[1], true);
}else{
System.out.println("Less or Too Much parameter!"+args.length);
}
}catch(Exception e){
System.err.println("ERROR");
}
}
/** Copy file <i>from</i> to file <i>to</i>
* @param from
* the source file.
* if <i>from</i> is a directory , copy all of its subdirectoris and file in it
* @param to
* the destination file
* @param overwrite
* if true the destination is overwritten. otherwise, source append to destination
* @exception FileNotFoundException
* @exception IOException
*/
public void copy2(String from, String to, boolean overwrite) throws FileNotFoundException, IOException{
File src = new File(from);
File dest = new File(to);
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest, !overwrite); byte[] buf = new byte[512];
int size = fis.read(buf);
while(size != -1) {
fos.write(buf, 0, size);
size = fis.read(buf);
}
fis.close();
fos.close();
}catch (FileNotFoundException fnfe){
if(src.isDirectory()) {
/* 原文件是目录 复制目录*/
File[] files = src.listFiles();
Stack stack = new Stack();
for(int i = 0; i < files.length; i++) {
dest.mkdirs();
System.out.println( files[i ]. getName() );
copy2(src + File.separator + files[ i].getName(), to + File.separator + files[ i].getName(), overwrite);
}
}else{
/* 原文件不存在或其它异常情况, 抛出异常*/
throw fnfe;
}
}catch (IOException e) {
/* 未知的I/O异常 */
throw e;
}
}
}