- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import java.io.InputStream;
- public class copy2 {
- /**
- * 把某盘中的一个多层文件夹拷贝到某盘中。用高效的方法
- *
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- File yuan = new File("e:\\qwer");
- File mudi = new File("f:\\qwer");
- copyfile(yuan,mudi);
- }
- private static void copyfile(File yuan, File mudi) throws IOException {
- // TODO Auto-generated method stub
- File[] file = yuan.listFiles();
- for(File f : file){
- //System.out.println(f);
- /*File yuandir =yuan;
- File mudidir = mudi;*/
- if(f.isDirectory()){
- File yuandir = new File(yuan+"\\"+f.getName());
- File mudidir = new File(mudi+"\\"+f.getName());
- copyfile(yuandir,mudidir);
- }
- else{
- File yuanfile = new File(yuan+"\\"+f.getName());
- File mudifile = new File(mudi+"\\"+f.getName());
- mudi.mkdirs();
- copyoi(yuanfile,mudifile);
- }
- }
-
- }
- private static void copyoi(File yuandir, File mudidir) throws IOException {
- // TODO Auto-generated method stub
- BufferedInputStream bufin = new BufferedInputStream(new FileInputStream(yuandir));
- BufferedOutputStream bufou = new BufferedOutputStream(new FileOutputStream(mudidir));
- byte[] buf = new byte[1024];
- int line =0;
- while((line = bufin.read(buf))!=-1){
- bufou.write(buf,0,line);
- bufou.flush();
- }
- bufin.close();
- bufou.close();
-
- }
- }
复制代码
是代码的问题吗?不是的话我真的是怀疑我的电脑了求解... |
|