开发者

Java: Does FileReader support finding files using strings with %20?

My apologies if this is too simple a question, I was unable to google it as it did not like searching for %20.

If I have a URL on which I use the getFile() method to obtain the path for a file I would like to open for processing. If the particular file resides in a directory that contains spaces, the path returned would contains %20 where the space should be.

Will a FileReader then be able to use the path as provided, or will I need to replace the %2开发者_Go百科0 with a space?


You will need to use URLDecoder yourself. FileReader just uses the String it's handed, and rightly so - %20 is a perfectly valid character sequence in a file name, and if it were automatically converted you could not access files containing it.


Use URLDecoder.decode() to decode a path


If you downloaded the file, and saved it on local file system. And you are using FileReader to read it as

FileReader fr = new FileReader(new File(url.getFile()));

Yes File, can understand URL encoding. So you don't need to decoded it. If you decoded it as others have suggested it will be more readable when you print the file path.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜