2015-07-20 126 views
-1

我越來越創建文件夾從文本

newDir D:\template_export\template\attachments\processed\enumeration\blocker.gif\enumeration\critical.gif\enumeration\high.gif\enumeration\low.gif\enumeration\major.gif\enumeration\medium.gif\enumeration\minor.gif\enumeration\normal.gif\enumeration\unassigned.gif\enumeration\unassigned2.gif\workflow\close.gif\workflow\defer.gif\workflow\duplicate.gif\workflow\inprogress.gif\workflow\new.gif\workflow\open.gif\workflow\reject.gif\workflow\remind.gif\workflow\reopen.gif\workflow\resolve.gif\workflow\unconfigure.gif\workflow\unresolve.gif\workflow\verify.gif\workflow\wontdo.gif\workflow\works.gif\workitemtype\bug.gif\workitemtype\enhancement.gif\workitemtype\general.gif\workitemtype\task.gif\workitemtype 
new directory false 
reached 
java.nio.file.NoSuchFileException: D:\template_export\template\attachments\1.gif -> D:\template_export\template\attachments\processed\enumeration\blocker.gif\enumeration\critical.gif\enumeration\high.gif\enumeration\low.gif\enumeration\major.gif\enumeration\medium.gif\enumeration\minor.gif\enumeration\normal.gif\enumeration\unassigned.gif\enumeration\unassigned2.gif\workflow\close.gif\workflow\defer.gif\workflow\duplicate.gif\workflow\inprogress.gif\workflow\new.gif\workflow\open.gif\workflow\reject.gif\workflow\remind.gif\workflow\reopen.gif\workflow\resolve.gif\workflow\unconfigure.gif\workflow\unresolve.gif\workflow\verify.gif\workflow\wontdo.gif\workflow\works.gif\workitemtype\bug.gif\workitemtype\enhancement.gif\workitemtype\general.gif\workitemtype\task.gif\workitemtype\unknown.gifprocess_template_license.htmltemplate.messagestemplate_en_US.messages 
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source) 
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source) 
    at sun.nio.fs.WindowsFileCopy.move(Unknown Source) 
    at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source) 
    at java.nio.file.Files.move(Unknown Source) 
    at Test.main(Test.java:60) 
Error. 

我的代碼是:

public class Test { 
    public static void main(String[] args) { 
     File orgDirectory = new File("D://template_export/template/attachments"); // replace this filename 
     // with the path to the folder 
     // that contains the original images 

     String fileContent = ""; 
     try (BufferedReader br = new BufferedReader(new FileReader(new File(orgDirectory, "attachments.txt")))) { 
      for (String line; 
      (line = br.readLine()) != null;) { 
       fileContent += line; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     String[] newLocations = fileContent.split(","); 
     File[] orgFiles = orgDirectory.listFiles(new FileFilter() {@Override 
      public boolean accept(File pathname) { 
       return pathname.getPath().endsWith(".gif"); 
      } 
     }); 
     File destinationFolder = new File("D://template_export/template/attachments/processed"); 
     if (!destinationFolder.exists()) { 

      System.out.println("here" + destinationFolder.mkdir()); 
     } 
     int max = Math.min(orgFiles.length, newLocations.length); 
     for (int i = 0; i < max; i++) { 
      String newLocation = newLocations[i]; 
      int lastIndex = newLocation.lastIndexOf("/"); 
      if (lastIndex == -1) { 
       continue; 
      } 
      String newDirName = newLocation.substring(0, lastIndex); 
      System.out.println("newDirName " + newDirName); 

      String newName = newLocation.substring(lastIndex); 
      System.out.println("newName " + newName); 

      File newDir = new File(destinationFolder, newDirName); 
      System.out.println("newDir " + newDir); 

      if (!newDir.exists()) { 
       System.out.println("new directory " + newDir.mkdir()); 
      } 
      try { 
       System.out.println("reached"); 

       Files.move(orgFiles[i].toPath(), new File(newDir, newName).toPath(), StandardCopyOption.REPLACE_EXISTING); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
+1

您從文件Concat的臺詞沒有','然後用','將它分割成newLocations(可能你在這一點上只獲得一個項目?) – griFlo

回答

0

你的attachments.txt文件的所有線串聯成一個單一的目標字符串。從異常消息判斷,看起來附件文件在每行末尾不包含逗號。因此,您最終會得到一個由單個文件名構成的目標,看起來就像是一個深度嵌套的目錄。

相反,我建議您一次將一行讀入ArrayList<String>,而不是連接並稍後拆分目的地。

ArrayList<String> newLocations = new ArrayList<>(); 
try (BufferedReader br = new BufferedReader(new FileReader(new File(orgDirectory, "attachments.txt")))) { 
    for (String line; (line = br.readLine()) != null;) { 
     newLocations.add(line); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

然後你可以使用newLocations.size(),你現在使用newLocations.length和使用newLocations.get(i)在您使用newLocations[i]

編輯:由於@griFlo在評論中指出,到把文件讀入行的列表一個簡單的選擇是:

List<String> newLocations = Files.readAllLines(Paths.get("attachments.txt")); 

如果該文件將無法正常使用UTF-8解碼讀取(其是上面的調用將使用什麼),或者如果你正在使用Java編譯7,你將不得不使用的readAllLines()兩個參數版本:

List<String> newLocations = Files.readAllLines(Paths.get("attachments.txt"), 
    StandardCharsets.US_ASCII); // or whatever encoding is appropriate 
+0

他可以使用'Files.readAllLines'方法('列表 allLines = Files.readAllLines(Paths.get(path));' – griFlo

+0

@griFlo - 是的,這是一個非常好的選擇。我會更新答案。 –