Indi
Indi

Reputation: 429

What is the difference between "Paths.get("").toAbsolutePath().toString()" and "System.getProperty("user.dir")" in java?

I am writing a java code to zip a directory (source)in a given location (destination). I have built the code and test cases up to this point. The doubt I have is about the path defining of destination and source paths while working with version control. To avoid conflicts which result in to build breaks, I have seen in a code I referred that the paths were set as follows;

Source Path-

zipRequest.setSource(Paths.get("").toAbsolutePath().toString() + "/src/main/resources/zipSource/source.txt");

and

Destination Path-

zipRequest.setDestination(System.getProperty("user.dir") + "/src/main/resources/zipDestination/destination.zip");

My questions are;

  1. What is the difference between the above two methods of a path defining?
  2. Is there any best practice to use the above methods? (Specifically for source or destination?
  3. Can I use one method for both path and destination?

Thanks in advance!

Upvotes: 3

Views: 726

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201447

Paths.get("").toAbsolutePath().toString() is the current working directory; where you were when you launched the program. It is exactly the same thing as System.getProperty("user.dir") and I would prefer the second one (it's shorter, and more obvious in what it does). But, since they are the same thing, you can use either (or both) for determining the source and destination.

Upvotes: 2

Related Questions