将名称从.txt映射到java中的文件夹

将名称从.txt映射到java中的文件夹,java,Java,我有一个名为附件的文件夹,其中包含5.gif图像,我有一个att.txt,其中包含此.gif图像的名称,现在我需要用att.txt中的名称重命名这些图像。 下面是我尝试的代码。请帮助 public static void main(String[] args) throws java.lang.Exception { java.io.BufferedReader br = new java.io.BufferedReader(new FileReader("D:\\template_ex

我有一个名为附件的文件夹,其中包含5.gif图像,我有一个att.txt,其中包含此.gif图像的名称,现在我需要用att.txt中的名称重命名这些图像。 下面是我尝试的代码。请帮助

public static void main(String[] args) throws java.lang.Exception {
    java.io.BufferedReader br = new java.io.BufferedReader(new FileReader("D:\\template_export\\template\\attachments"));
    String sCurrentLine="";
    while ((sCurrentLine = br.readLine()) != null) {
         sCurrentLine= sCurrentLine.replaceAll("txt", "gif");
         String[] s = sCurrentLine.split(",");
         for(int i=0;i<s.length;i++){
             new File("D:\\template_export\\template\\attachment_new"+s[i]).mkdirs();
             System.out.println("Folder Created");
         }
    }
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception{
java.io.BufferedReader br=new java.io.BufferedReader(新文件阅读器(“D:\\template\u export\\template\\attachments”);
字符串sCurrentLine=“”;
而((sCurrentLine=br.readLine())!=null){
sCurrentLine=sCurrentLine.replaceAll(“txt”、“gif”);
字符串[]s=sCurrentLine.split(“,”);
对于(int i=0;i请尝试以下方法:

public class Tst {

public static void main(String[] args) {
    File orgDirectory = new File("attachements");// 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, "attachements.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("processed");
    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);
        String newName = newLocation.substring(lastIndex);
        File newDir = new File(destinationFolder, newDirName);
        if (!newDir.exists()) {
            newDir.mkdir();
        }
        try {
            Files.move(orgFiles[i].toPath(), new File(newDir, newName).toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
}
它创建一个
文件
数组,其中包含源目录中的所有“gif”文件

以及:


将原始图像移动到新目录,并将其名称设置为从“attachements.txt”检索到的名称文件。

提供
att.txt
的内容会很有帮助/enumeration/blocker.gif/enumeration/critical.gif/workflow/close.gif/workflow/defer.gif…………我需要一个前三种类型的文件夹,它应该包含附件文件夹中的图像。/workitemtype/bug.gif/workitemtype/enhancement.gif过程\模板\许可证.html template.messages template_en_US.messages在当前代码中,它不是从附件文件夹中获取图像。这是所有这些
.gif
文件所在的同一文件夹中的
att.txt
。另外
/enumeration/blocker.gif
意味着我们需要将其放在
enumeration
文件夹中。请将您的问题重新表述为clearance.你为什么要在文件夹上调用
readLine
?你好,提图斯,我看到下面的错误……….java.nio.file.NoSuchFileException:D:\template\u export\template\attachments\0.gif->processed\enumeration\blocker.gif\enumeration\critical.gif\enumeration\high.gif\enumerumation\low.gif\enumeration\mair.gif\medium\minor.gif\chsmeration\normal.gif\enumeration\unassigned.gif\enumeration\unassigned2.gif\workflow\close.gif\workflow\defer.gif\workflow\inprogress.gif\workflow\new.gif\workflow\open.gif\workflow\embrand.gif\workflow\reopen.gif\workflow\workflow\resolve\unconfigure.gif\workfl@geetha这可能是因为“attachements.txt”没有我认为的格式(多个文件路径由一个空格分隔)。我没有足够的信息来解决这个问题,请尝试理解代码并修改它以适合您。
File[] orgFiles = orgDirectory.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            return pathname.getPath().endsWith(".gif");
        }
 });
Files.move(orgFiles[i].toPath(), new File(newDir, newName).toPath(), StandardCopyOption.REPLACE_EXISTING);