[英]Java : How to copy folder with contents from resource and copy to temp directory?
[英]how to copy the contents of a last modified file from a folder to the other folder using java?
我正在嘗試使用 java 將上次修改文件的內容從文件夾復制到另一個文件夾
我可以移動文件,但無法移動文件的內容
private File getLatestFilefromDir(String dirPath) throws IOException{
File dir = new File(dirPath);
File[] files = dir.listFiles();
if (files == null || files.length == 0) {
return null;
}
File lastModifiedFile = files[0];
for (int i = 1; i < files.length; i++) {
if (lastModifiedFile.lastModified() < files[i].lastModified()) {
lastModifiedFile = files[i];
}
}
String newFilePath = "C:\\newPath\\"+lastModifiedFile.getName();
Path temp = Files.move
(Paths.get(dirPath),
Paths.get(newFilePath ));
if(temp != null)
{
System.out.println("File renamed and moved successfully");
}
else
{
System.out.println("Failed to move the file");
}
return new File(newFilePath );
}
結果:只有文件在移動,但沒有內容
寧願使用ZE9713AE04A02A810D6F3D956F42794Z COMMONS ZCF382F382F1C43AB22BFFF0BFF0BFF0BFF0BD83251BZ 和更專門這是一個非常好的庫,非常適合您嘗試做的事情。
File sourceFile = new File(...);
File destinationFile = new File(...);
FileUtils.moveFile(sourceFile, destinationFile);
在我不久前做的一個小項目上使用它非常成功( FilingAssistant )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.