Java 从文本创建文件夹
我越来越Java 从文本创建文件夹,java,Java,我越来越 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\enumerat
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();
}
}
}
公共类测试{
公共静态void main(字符串[]args){
File orgDirectory=new File(“D://template\u export/template/attachments”);//替换此文件名
//使用文件夹的路径
//包含原始图像的
字符串fileContent=“”;
try(BufferedReader br=new BufferedReader(new FileReader(新文件(orgDirectory,“attachments.txt”))){
对于(弦线;
(line=br.readLine())!=null;){
fileContent+=行;
}
}捕获(例外e){
e、 printStackTrace();
}
字符串[]newLocations=fileContent.split(“,”);
File[]orgFiles=orgDirectory.listFiles(新的FileFilter(){@Override
公共布尔接受(文件路径名){
返回pathname.getPath().endsWith(“.gif”);
}
});
File destinationFolder=新文件(“D://template\u export/template/attachments/processed”);
如果(!destinationFolder.exists()){
System.out.println(“此处”+destinationFolder.mkdir());
}
int max=Math.min(orgFiles.length,newLocations.length);
对于(int i=0;i
您正在将attachments.txt
文件的所有行连接到一个目标字符串中。从异常消息判断,附件文件似乎没有在每行的末尾包含逗号。因此,最终的目标由一个文件名组成,似乎位于一个嵌套很深的目录中
相反,我建议您将一行一行地读入ArrayList
,而不是将目的地串联起来,然后再拆分
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();
}
如果使用UTF-8解码无法正确读取文件(这是上面调用将使用的),或者如果使用Java 7进行编译,则必须使用双参数版本的readAllLines()
:
List newLocations=Files.readAllLines(path.get(“attachments.txt”),
StandardCharsets.US_ASCII);//或任何适当的编码
您从文件中提取行而不使用,
,然后通过,
将其拆分为新位置(此时是否可能只获取一个项目?)他可以使用文件.readAllLines
方法(列出所有行=文件.readAllLines(path.get(path))是的,这是一个很好的选择。我会更新答案的。
List<String> newLocations = Files.readAllLines(Paths.get("attachments.txt"));
List<String> newLocations = Files.readAllLines(Paths.get("attachments.txt"),
StandardCharsets.US_ASCII); // or whatever encoding is appropriate