2

I've just faced an interesting bug. I tried to use user.home for saving path; The thing is the user.home in win xp is starting with

X:\Documents and Settings\

... sure the spaces take their place. The exception says

java.io.FileNotFoundException: C:\Documents%20and%20Settings ...

... so, as I can see, all spaces are replaced with '%'; I suppose it is an encoding problem but I am not pretty sure. The bug "5077403" page doesn't provide any work around; But I hope there should be some?

user592704
  • 3,674
  • 11
  • 70
  • 107

3 Answers3

1

Try updating the versions of the XML libraries you're using. Per the bug report you referenced, it is marked as fixed as of Java 1.5.

The %'s you're seeing are actually a valid URL encoding - with %20 representing a space.

Please see also some of the details I have posted around the versions of these libraries on the top of my personal blog post at http://blogger.ziesemer.com/2009/01/xml-and-xslt-tips-and-tricks-for-java.html .

ziesemer
  • 27,712
  • 8
  • 86
  • 94
0

The easiest and most rightful fix/ workaround for this bug is using decoding to replace the results system id.

streamResult.setSystemId(java.net.URLDecoder.decode(streamResult.getSystemId(), "UTF-8"));

This will decode the system Id and replace %20 with spaces.

Akalanka
  • 310
  • 5
  • 12
0

One hack (that should work on most Win XP systems) is to use something like this:

String homeDir = System.getProperty("user.home");
homeDir = homeDir.replace("Documents and Settings", "DOCUME~1");

This will leave homeDir intact if the code happens to be running on Windows 7 or a non-Windows environment.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • It's a quite interesting way thanks. But what if user.home may contain some unpredictable spaces on? I mean X:\Documents and Settings\xxx xx xx\xx xx x etc ? How to walk it around in a more flex way? – user592704 Nov 25 '11 at 05:46
  • @user592704 - The hack can be extended to use the DOS (so-called 8.3-format) names for every directory on the path. It's not straightforward, but with the help of a package called [NativeCall](http://johannburkard.de/software/nativecall/) and some Windows API calls, it can be done fairly easily, as described [here](http://dolf.trieschnigg.nl/eightpointthree/eightpointthree.html). – Ted Hopp Nov 25 '11 at 14:51
  • Well, I wouldn't call it a "hack" but a useful adaptation :) Thanks; But what about encoding? I didn't try but can encoding converting help? Have you got any tips about it? – user592704 Nov 25 '11 at 20:03
  • @user592704 - I'm not sure if it would help or not. Take a look at using the [URI class](http://docs.oracle.com/javase/6/docs/api/java/net/URI.html). – Ted Hopp Nov 26 '11 at 23:05
  • Emm... can it convert % etc symbols back to spaces? – user592704 Nov 28 '11 at 01:04
  • @user592704 - I believe it can be used for that. `getPath()` is supposed to return the decoded path of a URI. – Ted Hopp Nov 28 '11 at 03:27