2

Reading the documentation on Path and other places it's obvious to me that it always takes the file system the VM is running on. However, I want to make it clear to Java that I want to have Unix-paths.

The reason is that I'm exporting paths as JSON via Jackson and there using toString() in a serializer returns different results for different VMs. In simple terms I want to get this even if I'm developing on a Windows machine:

{"path":"/tmp"}

My serializer looks like this:

public class PathSerializer extends JsonSerializer<Path> {

  @Override
  public void serialize(Path path, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
    jsonGenerator.writeString(path.toString());
  }

}

To solve it for Windows I could do this of course:

public class PathSerializer extends JsonSerializer<Path> {

  @Override
  public void serialize(Path path, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
    jsonGenerator.writeString(path.toString().replace('\\', '/'));
  }

}

But, that's not independent of the file system. I mean I know what target system I have and I don't want to cover all source systems here.

How do I do that? I mean the last resort of course is to use String instead of Path, but that's kind of lame IMHO.

3
  • what value contains your path variable ?? is it like that ?? eg:- foldername\\foldername\\filename Commented Mar 18, 2016 at 8:44
  • @VikrantKashyap An absolute path. Currently the JSON would look like {"path":"\\tmp"} Commented Mar 18, 2016 at 8:53
  • your questions is valid . If your production system is deployed over 'linux` and User System is windows based then it will raise an error Commented Mar 18, 2016 at 8:58

2 Answers 2

1

This will work on all platforms:

@Override
public void serialize(Path path, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException
{
   jsonGenerator.writeString(path.toString().replace(File.separator, "/"));
}

With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally. That being said no matter what OS will later on read the path will be able to construct it correctly.

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

1 Comment

I think this is the closest I can get to a "path generator for a different file system".
1

The trick is to use the URI in between..

// At the begining you have the input parameter with tmp path in it
// so same as:
Path path= new File("/tmp").toPath();
// System.out.println(path.toString()) 
// -> "\tmp" under windows
URI theTmpFolderURI = path.toUri();
// System.out.println(theTmpFolderURI.toString()) 
// -> "file:///C:/tmp" under windows and 
// -> "file://tmp/" under xNIX     

Back to a Path and then a file the result is environment dependent under windows you got

Paths.get(theTmpFolderURI).toFile().getAbsolutePath();
// -> C:\tmp

Same as :

Paths.get(theTmpFolderURI).toAbsolutePath().toString();
// -> C:\tmp

So the code will be something like:

String fromString = Paths.get(path.toUri()).toAbsolutePath().toString(); 
jsonGenerator.writeString(fromString);

and the resulting JSON will be

"C:\\tmp"

5 Comments

Where does it get rid of the backslash?
For the communication between systems always uses the URI notation. That's the purpose of the standard obviously :) Internally (server side) the code will adapt the the current environment automatically and transform the URI to specific filesystem resource identifiers at runtime. The double backslash in the JSON is mandatory for the JSON to be VALID....
If you need to force the filesystem to a specific environment I recomment using the Apache Commons IO project's tooling like this method for windows style conversion or this other for the unix style.
No, that's not what I want, I think you misunderstand the question. I don't need full URIs, just the path and I don't need any backslashes anywhere.
Have a look to the Apache Commons IO FilenameUtils. There is some nice windows to Unix and reverse conversation tools. commons.apache.org/proper/commons-io/javadocs/api-1.4/org/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.