3

I'm trying to open files from within Java with something like this:

java.awt.Desktop.getDesktop().open(new File("c:\\coolfile.txt");

Of course it all works fine and dandy in most cases.

HOWEVER!

When I have a file with the unicode character u3000, I cannot open it! Even if the file exists.

For example:

java.awt.Desktop.getDesktop().open(new File("c:\\coolfile\u3000withweirdname.txt");

I get an Exception, EVEN WHEN THE FILE EXISTS

[java] java.io.IOException: Failed to open file:/E:/_prog/test%E3%80%80.txt. Error message: The system cannot find the path specified.

Please help me i tried pretty much everything. This is driving me insane :/

Edit:

To give some more info:

I can easily create file with this name from Java.

It seems it has something to do with whitespace

I don't know if it applies to other characters; I didn't find any yet. But of course if there's 1 there could easily be 100.

I'm pretty sure I can't read from the file or write to it from Java, but I haven't tested that since it isn't my main concern.

Dave L.
  • 9,595
  • 7
  • 43
  • 69
user1258312
  • 49
  • 1
  • 7

3 Answers3

1
java.awt.Desktop.getDesktop().open(new File("c:\\coolfile\u3000withweirdname.txt");

That doesn't compile. Clearly it isn't your real code.

[java] java.io.IOException: Failed to open file:/E:/_prog/test%E3%80%80.txt

And there is proof. Clearly you passed a URL to new FileInputStream(). It doesn't take a URL string, it takes a file name.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • "Clearly you passed a URL to new FileInputStream()" this is wrong. I got the file path from calling getFiles() on a folder – user1258312 Mar 09 '12 at 21:21
  • @user1258312 Not proven. Show us the code. Where did the file:// and the %-escapes come from if it wasn't a URL? – user207421 Mar 10 '12 at 00:26
  • i don't know know i assumed it would come when formatting the exception for display or something. I realize now i should have made a compilable example. The reason i did not post the project is that its way too big and would just clutter the discussion up. – user1258312 Mar 10 '12 at 00:59
  • ok man i posted a fix now. I might have made some dumb mistake somewhere else but trust me i didn't enter URL i entered correct file name. – user1258312 Mar 10 '12 at 01:16
1

Ok i think i actually found a kind of solution to my question and i post it here to help any people that may have similar problems.

This fix only works for Windows (XP and up i think) BUT i don't even know if this problem exist in other OS. And even if it does a similar fix should be possible.

I am using the following code to succesfully open a file with the character:

Process p = new ProcessBuilder("cmd", "/c start \"\" \"E:\_prog\test\u3000.txt\"").start();

Which opens the file 'E:_prog\testu3000.txt'

user1258312
  • 49
  • 1
  • 7
0

As far as i know \u3000 is ideographic space character. To test your code I created a file with name CompanyAlt+3000Address.

Note: when you press Alt+3000, windows will create an ideographic space character. Then I copied the file name, to my java program and it worked for me.

Desktop.getDesktop().open(new File("C:\\Users\\Chandru\\Desktop\\Company╕Address.txt")); 
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125