Stainedart
Stainedart

Reputation: 1969

Window/linux path component separating

I am writing a path separator in my linux environment and it is meant to parse a file path that can be both linux and windows style which I never know before hand. Currently I was splitting using the File.separator but that only works for the platform I am on...

I am looking to know if there is a way to handle this using something like the Path object for example.

[...]
Node pathElement = nodeList.item(nodePos);
String path = pathElement.getTextContent();
String[] pathElements = path.split(File.separator);
String contentFileName = pathElements[pathElements.length - 1];
String parentFolderPath = xmlFiles[u].getParentFile().getAbsolutePath();
pathElement.setTextContent(parentFolderPath + File.separator + contentFileName);
[...]

EDIT: I need to be able to break a windows style path from linux and vice versa. I have also attempted to use Paths.getPath(...) unsuccessfully

See a sample output from the above code:

/input/ZIP_0bd78143-f6cd-473f-84f7-d0da09dc42aa/7634/c:\SharedFiles\7634\1

It used the windows path as file name since it wasn't able to break it. I am trying to avoid fancy logic to detect the type of path if possible.

Upvotes: 0

Views: 525

Answers (1)

dardo
dardo

Reputation: 4960

So based on your edits, you might need to detect the FileSystem at a given URI, might want to take a look into this:

http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSystems.html

Upvotes: 1

Related Questions