60

Rewrote question with more info

I have some code that creates a Path object using relative paths, like this: Paths.get("..", "folder").resolve("filename"). Later, I want to get the path string "..\folder\filename" from it (I'm on windows, so backslashes). When I run this code using manual compile or from Eclipse, this works fine.

However, when I run it using Maven, it doesn't work any more. The toString() method returns [.., folder, filename] instead of an actual path string. Using path.normalize() doesn't help. Using path.toFile().getPath() does return what I'm looking for, but I feel there should be a solution using just the nio.path API.

6
  • 2
    That doesn't seem right. Both Paths.get("path/to/file").toString() and Paths.get("path", "to", "file").toString() both return "path/to/file" for me. Commented Jul 9, 2013 at 15:38
  • Same is true of Paths.get("path/to").resolve("file").toString(), which returns "path/to/file". Commented Jul 9, 2013 at 15:39
  • I may be wrong but for me both returns the same string !!! Commented Jul 9, 2013 at 15:39
  • 1
    What result do you see for Paths.get("path", "to", "file").getClass().getName()? Perhaps you're picking up a wonky FileSystem implementation. Commented Jul 9, 2013 at 15:43
  • The difference might be that I'm starting with going up one path (relative to working directory): Paths.get("..", "path/to", "file"). I didn't think it would be relevant to the question, but apparently it is. Commented Jul 9, 2013 at 15:45

2 Answers 2

72

Use:

Paths.get(...).normalize().toString()

Another solution woul be:

Paths.get(...).toAbsolutePath().toString()

However, you get strange results: Paths.get("/tmp", "foo").toString() returns /tmp/foo here. What is your filesystem?

Sign up to request clarification or add additional context in comments.

3 Comments

Updated question with more info on when this fails.
Are you sure this is java.nio.Paths that you use?
Yes, I'm sure :) java.nio.file.Paths, to be exact.
9

To complete the the fge's answer, I would add some info:

  1. normalize() simply removes redundant pieces of strings in your path, like . or ..; it does not operate at the OS level or gives you an absolute path from a relative path
  2. toAbsolutePath() on the contrary gives you what the name says, the absolute path of the Path object. But...
  3. toRealPath() resolves also soft and hard links (yes they exists also on Windows, so win user, you're not immune). So it gives to you, as the name says, the real path.

So what's the best? It depends, but personally I use toRealPath() the 99% of the cases.

As pointed out by Roberto Bonvallet, since toRealPath() throws an exception if the file does not already exist because, for example, you want to create it. In this case I prefer toAbsolutePath().

Source: Official javadoc

2 Comments

Note that toRealPath throws an exception if the file doesn't exist, which is not what you want when you are passing the path for a file yet to be created.
Thank you @RobertoBonvallet, I'll add it to the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.