Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java File can't open certain files

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.

like image 369
user1258312 Avatar asked Feb 22 '26 22:02

user1258312


2 Answers

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.

like image 99
user207421 Avatar answered Feb 24 '26 11:02

user207421


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'

like image 26
user1258312 Avatar answered Feb 24 '26 13:02

user1258312