2013-04-07 33 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