我有一个看起来像这样的文本文件fileName | path
。我阅读了文本文件并将其拆分为|
. 现在我想使用文件名和路径将文件从一个目录复制到另一个目录。
这是我到目前为止所拥有的,我得到的结果如下:
file to be copied: test.jar to path: c:/test
c:\InstallFiles\test.jar
c:\test
c:\test
这是我的代码:
String record = "";
FileReader fileReader = null;
String curDir = System.getProperty("user.dir");
File file = new File(curDir + "/InstallFiles.txt");
File installFiles = new File("c:/InstallFiles");
File[] files = installFiles.listFiles();
try {
fileReader = new FileReader(file);
BufferedReader myInput = new BufferedReader(fileReader);
while ((record = myInput.readLine()) != null) {
String[] recordColumns = record.split("\\|");
String fileName = recordColumns[0].toString().trim();
String path = recordColumns[1].toString().trim();
System.out.println("file to be copied: " + fileName + " to path: " + path);
Path source = Paths.get(installFiles +"/"+ fileName);
System.out.println(source);
Path target = Paths.get(path);
System.out.println(target);
Files.copy(source, target);
System.out.println("Copied file: " + fileName + " to " + path);
}
myInput.close();
fileReader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}