[WIP] FileUtil: copy files using Files methods#1907
Conversation
| boolean res = false; | ||
| try { | ||
| res = FileUtil.copyFile(file, tmpFile.toFile(), true); | ||
| res = FileUtil.copyFile(Paths.get(file.toURI()), tmpFile, true); |
There was a problem hiding this comment.
Since tmpFile is already a Path object, why don't you replace File file with Path file?
|
Can you please try to refactor |
|
Nice that you want to take car of it, well a couple of days ago I already had the same idea: 😆 |
|
@Siedlerchr thx for the notice |
…feedbacks of tobias boceckts have done
| try { | ||
| renameSuccessful = FileUtil.renameFile(Paths.get(expandedOldFilePath), Paths.get(newPath)); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); |
There was a problem hiding this comment.
Please use/introdue logging with the Logger class and then log the excpetion with LOGGER.error("message", ex)
tschechlovdev
left a comment
There was a problem hiding this comment.
Please fix travis, seems to be a problem in the RenamePdfCleanup.
| // Check if the file already exists. | ||
| if (dest.exists() && !deleteIfExists) { | ||
| if (!Files.exists(pathToSourceFile)) { | ||
| return false; |
There was a problem hiding this comment.
I think it would be nice if you would log here, why the file wasn't copied. So here because the path to the source file not exists.
| } | ||
| out.flush(); | ||
| if (Files.exists(pathToDestinationFile) && !replaceExisting) { | ||
| return false; |
There was a problem hiding this comment.
Logging here would be also nice.
| out.write(el); | ||
| } | ||
| out.flush(); | ||
| if (Files.exists(pathToDestinationFile) && !replaceExisting) { |
There was a problem hiding this comment.
Could you add a test case for the left part of the if coniditon? Or reference it if you have already one, because I don't see it.
There was a problem hiding this comment.
the testcase is:
testCopyFileFromEmptySourcePathToEmptyDestinationPathWithOverrideExistFile
| boolean renameSuccessful = FileUtil.renameFile(expandedOldFilePath, newPath); | ||
| boolean renameSuccessful = false; | ||
| try { | ||
| renameSuccessful = FileUtil.renameFile(Paths.get(expandedOldFilePath), Paths.get(newPath)); |
There was a problem hiding this comment.
I think it would be better to place the try/catch construct in FileUtil.renameFile, because the exception is actually thrown by Files.move(...)
| if (!Files.exists(fromFile) || !Files.exists(toFile)) { | ||
| return false; | ||
| } | ||
| return Files.move(fromFile, fromFile.resolveSibling(toFile), StandardCopyOption.REPLACE_EXISTING) != null; |
There was a problem hiding this comment.
remove throws IOException and add try/catch here.
| LOGGER.error("Problem copying file", e); | ||
| return false; | ||
| } | ||
| FileUtil.copyFile(Paths.get(fileName), Paths.get(destFile.toURI()), true); |
There was a problem hiding this comment.
For the second argument you can use the method toPath() of the File class
| // repeating the action will have a different result. | ||
| // On the other hand, our temporary file should still be clean, and won't be deleted. | ||
| throw new SaveException("Save failed while committing changes: " + ex2.getMessage(), | ||
| Localization.lang("Save failed while committing changes: %0", ex2.getMessage())); |
There was a problem hiding this comment.
Please remove this string from the localization files. This should fix the failing travis build. Otherwise LGTM
* Remove Localization.lang "Save failed while committing changes: %0" from the localization files * Use toPath() on the second argument of the method FileUtil.copyFile(...) in class DroppedFileHandler
|
@Siedlerchr Hiho i have done the feedbacks,. Please a look and merge it please 😄 |
Siedlerchr
left a comment
There was a problem hiding this comment.
Good 👍
If you please fix the one log message, I'm happpy and I merge it in!
| try { | ||
| entry.updateTimeStamp(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); |
* Insert LOGGER.error instead of e.printStackTrace() in method updateTimeStamp(String key) in class FileUpdateMonitor
|
Just merged it in! 👍 |
* FileUtil: copy files using Files methods * Refactor renameFile, create testcase for copyFile and renameFile and feedbacks of tobias boceckts have done * Create extra testcase incl. feedbacks * Refactoring_19.09.2016 - Dennis feedback * Feedback_21.09.2016: * Remove Localization.lang "Save failed while committing changes: %0" from the localization files * Use toPath() on the second argument of the method FileUtil.copyFile(...) in class DroppedFileHandler * Feedback_21.09.2016_13:50: * Insert LOGGER.error instead of e.printStackTrace() in method updateTimeStamp(String key) in class FileUpdateMonitor
Jabref now copies files by using java.nio package instead of java.io
See: JabRef#23