Skip to content

[WIP] FileUtil: copy files using Files methods#1907

Merged
Siedlerchr merged 8 commits into
JabRef:masterfrom
motokito:usingFilesMethods
Sep 21, 2016
Merged

[WIP] FileUtil: copy files using Files methods#1907
Siedlerchr merged 8 commits into
JabRef:masterfrom
motokito:usingFilesMethods

Conversation

@motokito

@motokito motokito commented Sep 1, 2016

Copy link
Copy Markdown
Contributor

Jabref now copies files by using java.nio package instead of java.io

See: JabRef#23

boolean res = false;
try {
res = FileUtil.copyFile(file, tmpFile.toFile(), true);
res = FileUtil.copyFile(Paths.get(file.toURI()), tmpFile, true);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since tmpFile is already a Path object, why don't you replace File file with Path file?

@boceckts

boceckts commented Sep 1, 2016

Copy link
Copy Markdown
Contributor

Can you please try to refactor renameFile(String fileName, String destFilename) to use java nio as well?

@Siedlerchr

Siedlerchr commented Sep 1, 2016

Copy link
Copy Markdown
Member

Nice that you want to take car of it, well a couple of days ago I already had the same idea: 😆
Feel free to use that for your changes // Edit: There is already code for renaming and a test file
#1861

@boceckts

boceckts commented Sep 1, 2016

Copy link
Copy Markdown
Contributor

@Siedlerchr thx for the notice
@motokito please add tests for your changes/modify existing ones (if applicable).

try {
renameSuccessful = FileUtil.renameFile(Paths.get(expandedOldFilePath), Paths.get(newPath));
} catch (IOException e) {
e.printStackTrace();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use/introdue logging with the Logger class and then log the excpetion with LOGGER.error("message", ex)

@tschechlovdev tschechlovdev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

}
out.flush();
if (Files.exists(pathToDestinationFile) && !replaceExisting) {
return false;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logging here would be also nice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

out.write(el);
}
out.flush();
if (Files.exists(pathToDestinationFile) && !replaceExisting) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the testcase is:
testCopyFileFromEmptySourcePathToEmptyDestinationPathWithOverrideExistFile

boolean renameSuccessful = FileUtil.renameFile(expandedOldFilePath, newPath);
boolean renameSuccessful = false;
try {
renameSuccessful = FileUtil.renameFile(Paths.get(expandedOldFilePath), Paths.get(newPath));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to place the try/catch construct in FileUtil.renameFile, because the exception is actually thrown by Files.move(...)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

if (!Files.exists(fromFile) || !Files.exists(toFile)) {
return false;
}
return Files.move(fromFile, fromFile.resolveSibling(toFile), StandardCopyOption.REPLACE_EXISTING) != null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove throws IOException and add try/catch here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

LOGGER.error("Problem copying file", e);
return false;
}
FileUtil.copyFile(Paths.get(fileName), Paths.get(destFile.toURI()), true);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the second argument you can use the method toPath() of the File class

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

// 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()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this string from the localization files. This should fix the failing travis build. Otherwise LGTM

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done !

* 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
@boceckts boceckts added the status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Sep 21, 2016
@motokito

Copy link
Copy Markdown
Contributor Author

@Siedlerchr Hiho i have done the feedbacks,. Please a look and merge it please 😄

@Siedlerchr Siedlerchr left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use LOGGER.error...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 👍

* Insert LOGGER.error instead of e.printStackTrace() in method updateTimeStamp(String key) in class FileUpdateMonitor
@Siedlerchr Siedlerchr merged commit bddca15 into JabRef:master Sep 21, 2016
@Siedlerchr

Copy link
Copy Markdown
Member

Just merged it in! 👍

@motokito motokito deleted the usingFilesMethods branch September 21, 2016 12:00
zesaro pushed a commit to zesaro/jabref that referenced this pull request Nov 22, 2016
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants