I have a text file that looks like this fileName | path
.
I read the text file and split it at the |
.
Now I want to use the filename and the path to copy files from one directory to another.
Here is what I have so far, the result I get is as follows:
file to be copied: test.jar to path: c:/test
c:\InstallFiles\test.jar
c:\test
c:\test
here is my code:
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());
}
}