2013-04-07 57 views
1

我有文本文件包含圖像文件的網址。複製給定它的文件url

我想從其他目錄複製這些文件。

此代碼不能正常工作

File source =// 
    File target = // 
    File urls = // 
    Scanner scanner = new Scanner(urls); 
    for (File child :source.listFiles()) 
    { 
     if (child.isDirectory()) 

      while (scanner.hasNextLine()) { 

      String line = scanner.nextLine(); 

      for (File childOfchild:child.listFiles()) 
      { 
       if (childOfchild.getAbsolutePath().contains(line)) 

           FileUtils.copyFileToDirectory(childOfchild,target); 

      } 

      } 
    } 

問題是什麼?

的第一個文件包含圖像的URL,我想複製

\actor\0211_2233188435.jpg 
    \actor\0405_52447453.jpg 

源位置包含704個子目錄和250000頁的文件 爲examlpe

/media/B68E392F8E38E98F/Flickr1/Flickr/actor/0001_2124494179.jpg 
+2

什麼是錯誤的?現在的位置...... – 2013-04-07 10:27:41

+0

該文件包含175000個網址 且只有40個圖像被複制 – nawara 2013-04-07 10:29:52

+0

您的操作系統是什麼? – linski 2013-04-07 10:33:29

回答

0

誤差的同時指令

File source =// 
File target = // 
File urls = // 
Scanner scanner = new Scanner(urls); 
while (scanner.hasNextLine()) { 
for (File child :source.listFiles()) 
{ 
    if (child.isDirectory()) 



     String line = scanner.nextLine(); 

     for (File childOfchild:child.listFiles()) 
     { 
      if (childOfchild.getAbsolutePath().contains(line)) 

          FileUtils.copyFileToDirectory(childOfchild,target); 

     } 

     } 
} 
0

使用遞歸,在網你可以找到例子。 This is one of them:

public void copyDirectory(File sourceLocation , File targetLocation) throws IOException { 
    if (sourceLocation.isDirectory()) { 
     if (!targetLocation.exists()) { 
      targetLocation.mkdir(); 
     } 

     String[] children = sourceLocation.list(); 
     for (int i=0; i<children.length; i++) { 
      copyDirectory(new File(sourceLocation, children[i]), 
        new File(targetLocation, children[i])); 
     } 
    } else { 

     InputStream in = new FileInputStream(sourceLocation); 
     OutputStream out = new FileOutputStream(targetLocation); 

     // 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(); 
    } 
} 
+0

此代碼將所有文件從一個目錄複製到另一個 我需要複製我擁有它的文件網址 我會將我的問題編輯爲更清晰 – nawara 2013-04-07 10:45:25

0

下面副本的代碼使用順序方法的sourceDir到DESTDIR的整個文件結構。我對apache-commons庫不熟悉,所以有可能有更優雅的解決方案。

public static String getNewPath(File file,File source,File dest) throws IOException { 
    String filePath = file.getCanonicalPath().replaceAll(source.getCanonicalPath(), "");   
    return dest.getCanonicalPath()+filePath; 
} 

public static void main(String[] args) throws IOException { 
    File sourceDir = new File("./f_src"); 
    File destDir = new File("./f_dest"); 
    Collection<File> fs = FileUtils.listFilesAndDirs(sourceDir, TrueFileFilter.TRUE, TrueFileFilter.TRUE); 
    for (File f : fs) { 
     if (f.exists() && f.canRead()) { 
      if (f.isFile()) { 
       FileUtils.copyFile(f, new File(getNewPath(f,sourceDir,destDir)));      
      } 
      else { 
       FileUtils.copyDirectory(f, new File(getNewPath(f,sourceDir,destDir))); 
      } 
     } 
    } 
} 

的代碼假設你的項目根目錄中有兩個目錄:即f_srcf_dest。這是我的測試文件結構f_src,數字目錄和信件文件:

f_src 
    |\-1 
    | |\-2 
    | | | \-3 
    | | | \-a 
    | | \-b 
    | \-c 
    |\-4 
    | |\-5 
    | | \-d 
    | \-e 
    |\-6 
    |\-7 
    | \-f 
    \-g