import java.io.* ;//method public void copyTo(String srcDir,String desDir){ File ds = new File(srcDir) ; ds.renameTo(desDir) ; }
你也可以在java中调用xcopy命令
不是用DOS命令,用其他的办法可以吗
拷贝目录,下例只可以拷贝目录中的文件:/** * This class copies an input files of a directory to another directory not include subdir * * @param String sourcedir the directory to copy from such as:/home/bqlr/images * @param String destdir the target directory */ public static void CopyDir(String sourcedir,String destdir) throws Exception { File dest = new File(destdir); File source = new File(sourcedir); String [] files= source.list(); try { destdir.mkdirs(); }catch (Exception ex) { throw new Exception("CopyDir:"+ex.getMessage()); } for (int i = 0; i < files.length; i++) { String sourcefile = source+File.separator+files[i]; String destfile = dest+File.separator+files[i]; File temp = new File(sourcefile); if (temp.isFile()){ try{ copy(sourcefile,destfile); }catch (Exception ex) { throw new Exception("CopyDir:"+ex.getMessage()); } } } } 拷贝文件,相当于linux中cp命令,但与平台无关,可以拷贝文本 或二进制文件:/** * This class copies an input file to output file * * @param String input file to copy from * @param String output file */ public static boolean copy(String input, String output) throws Exception{ int BUFSIZE = 65536; try{ FileInputStream fis = new FileInputStream(input); FileOutputStream fos = new FileOutputStream(output); int s; byte[] buf = new byte[BUFSIZE]; while ((s = fis.read(buf)) > -1 ){ fos.write(buf, 0, s); } }catch (Exception ex) { throw new Exception("makehome"+ex.getMessage()); } return true; }
public void copyTo(String srcDir,String desDir){
File ds = new File(srcDir) ;
ds.renameTo(desDir) ;
}
* This class copies an input files of a directory to another directory not include subdir
*
* @param String sourcedir the directory to copy from such as:/home/bqlr/images
* @param String destdir the target directory
*/
public static void CopyDir(String sourcedir,String destdir) throws Exception
{
File dest = new File(destdir);
File source = new File(sourcedir); String [] files= source.list();
try
{
destdir.mkdirs();
}catch (Exception ex) {
throw new Exception("CopyDir:"+ex.getMessage());
}
for (int i = 0; i < files.length; i++)
{
String sourcefile = source+File.separator+files[i];
String destfile = dest+File.separator+files[i];
File temp = new File(sourcefile);
if (temp.isFile()){
try{
copy(sourcefile,destfile);
}catch (Exception ex) {
throw new Exception("CopyDir:"+ex.getMessage());
}
}
}
}
拷贝文件,相当于linux中cp命令,但与平台无关,可以拷贝文本 或二进制文件:/**
* This class copies an input file to output file
*
* @param String input file to copy from
* @param String output file
*/
public static boolean copy(String input, String output) throws Exception{
int BUFSIZE = 65536;
try{
FileInputStream fis = new FileInputStream(input);
FileOutputStream fos = new FileOutputStream(output); int s;
byte[] buf = new byte[BUFSIZE];
while ((s = fis.read(buf)) > -1 ){
fos.write(buf, 0, s);
} }catch (Exception ex) {
throw new Exception("makehome"+ex.getMessage());
}
return true;
}