2012-12-13 28 views
2

我試圖製作複製目錄的子項目的程序,但我無法標記所有特定名稱,因爲它們在每個文件夾中都有所不同。這是我的代碼,但是如果源是「C:\ src」,輸出是「C:\ dst」,它會創建文件夾「C:\ dst \ src(children files)」,但我想make「C:\ dst(子文件)」。誰能幫忙?在Java中將源文件的子文件複製到目的地

public static void copy(File source, File destination) throws IOException { 
    if (source == null) { 
     throw new NullPointerException("Null Source"); 
    } 
    if (destination == null) { 
     throw new NullPointerException("Null Destination"); 
    } 
    if (source.isDirectory()) { 
     copyDirectory(source, destination); 
    } else { 
     copyFile(source, destination); 
    } 
} 
//converts to location 
public static void copyDirectory(File source, File destination) throws IOException { 
    copyDirectory(source, destination, null); 
} 

public static void copyDirectory(File source, File destination, FileFilter filter) throws IOException { 
    File nextDirectory = new File(destination, source.getName()); 
    if (!nextDirectory.exists() && !nextDirectory.mkdirs()) {// create the directory if necessary... 
     Object[] filler = {nextDirectory.getAbsolutePath()}; 
     String message = "Dir Copy Failed"; 
     throw new IOException(message); 
    } 
    File[] files = source.listFiles(); 
    for (int n = 0; n < files.length; ++n) {// and then all the items below the directory... 
     if (filter == null || filter.accept(files[n])) { 
      if (files[n].isDirectory()) { 
       copyDirectory(files[n], nextDirectory, filter); 
      } else { 
       copyFile(files[n], nextDirectory); 
      } 
     } 
    } 
} 

public static void copyFile(File source, File destination) throws IOException { 
    // what we really want to do is create a file with the same name in that dir 
    if (destination.isDirectory()) { 
     destination = new File(destination, source.getName()); 
    } 
    FileInputStream input = new FileInputStream(source); 
    copyFile(input, destination); 
} 

public static void copyFile(InputStream input, File destination) throws IOException { 
    OutputStream output = null; 
    try { 
     output = new FileOutputStream(destination); 
     byte[] buffer = new byte[1024]; 
     int bytesRead = input.read(buffer); 
     while (bytesRead >= 0) { 
      output.write(buffer, 0, bytesRead); 
      bytesRead = input.read(buffer); 
     } 
    } catch (Exception e) { 
     // 
    } finally { 
     input.close(); 
     output.close(); 
    } 
    input = null; 
    output = null; 
} 
+1

如果你這樣做不僅僅是腦部運動,我應該警告你,你可以在命令行中的一行。 'cp -r src/*。/ dst' – thatidiotguy

回答

1

更換

if (source.isDirectory()) { 
    copyDirectory(source, destination); 
} else { 
    copyFile(source, destination); 
} 

通過

if (source.isDirectory()) { 
    for (File child : source.listFiles()) { 
     if (child.isDirectory()) { 
      copyDirectory(child, destination); 
     } else { 
      copyFile(child, destination); 
     } 
    } 
} else { 
    copyFile(source, destination); 
} 
0
+0

您是否可以始終依靠目標系統上安裝的commons-io,還是必須將其包含在項目中? –

+0

commons-io是第三方庫。你必須將它包含在你的項目中。 –

0

使用getParentFile()來獲得父目錄:

if (source.isDirectory()) { 
    copyDirectory(source, destination.getParentFile()); 
} else { 
    copyFile(source, destination.getParentFile()); 
} 
0

試試這一個。將文件夾中的整個文件從源文件複製到目標文件。

import java.io.*; 

public class copydir 
{ 
public static void main(String args[]) 
{ 
    File srcFolder = new File("E://Paresh/programs/test"); 
    File destFolder = new File("D://paresh"); 

    if(!srcFolder.exists()) 
    { 

      System.out.println("Directory does not exist."); 
      //just exit 
     System.exit(0); 
    } 
    else{ 

      try{ 
       copyDirectory(srcFolder,destFolder); 
         } 
      catch(IOException e) 
      { 
        e.printStackTrace(); 
        //error, just exit 
         System.exit(0); 
       } 
     } 
    System.out.println("Done"); 
} 

public static void copyDirectory(File src , File target) throws IOException 
{ 
    if (src.isDirectory()) 
    { 
      if (!target.exists()) 
     { 
       target.mkdir(); 
      } 

      String[] children = src.list(); 
      for (int i=0; i<children.length; i++) 
     { 
       copyDirectory(new File(src, children[i]),new File(target, children[i])); 
      } 
    } 
    // if Directory exists then only files copy 
    else 
    { 

     InputStream in = new FileInputStream(src); 
     OutputStream out = new FileOutputStream(target); 

     // Copy the bits from instream to outstream 
     byte[] buf = new byte[1024]; 
     int len; 
     while ((len = in.read(buf)) > 0) 
     { 
        out.write(buf, 0, len); 
     } 
     in.close(); 
     out.close(); 
     } 
    }  

} 
相關問題