Junge Junge - 1 year ago 59
Java Question

Reading a Text file from Path beginning with /// and containing a colon

I am, as stated in the headline, trying to read a file from a path given to me as a String, located in a Tomcat-Context file.

The path starts with "///C:". On Unix systems it would start with "/C:".
As of now I am trying to do as follows:

MyPath = Paths.get("///C:" + filename);
byte[] encoded = Files.readAllBytes(MyPath);
String JsonString = new String(encoded);

this leads to this error: "Illegal character [:] in path at index 4: ///C:/file.json"

I know Windows doesnt like Colons in its Paths, but when I cut the leading Substirng "///" from the Path, windows can handle it. (But that doesn't realy help me, since this is fragile and a realy bad way of coding. Also it doesn't work on half of the servers)

I am pretty shure this is just me not understanding the path-naming-convention.
So in what way should this kind of paths be handled?


Answer Source

My mistake was, that I did not treat the URL I received as an URL. The following code snippet did the trick:

MyPath = Paths.get( new URL("///C:" + filename).toURI() );

It works on both systems. I think Paths.get() understands the "///" only in URLs.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download