使用Java读取名称中有空格的文件? [英] Read file with whitespace in name using Java?
本文介绍了使用Java读取名称中有空格的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
文件原名是1_00100 0042.jpg
.我有问题:
The original name of file is 1_00100 0042.jpg
. I have a problem:
java.net.URISyntaxException: Illegal character in path at index 49: file:///opt/storage/user-data/attachments/1_00100\ 0042.jpg
你能给我一些如何使用这个错误路径获取这个文件的解决方案吗?我知道 C# 有 Path 类.Java中有没有类似的东西?
Can you give me some solutions how to get this file using this bad path? I know that C# have Path class. Is there something similar in Java?
我尝试下一步但没有成功:
I tried to do next but not successfully:
private String replaceWhitespace(String str) {
if (str.contains(" ")) {
str = str.replace(" ", "%20");
}
return str;
}
推荐答案
使用文件,它适用于空格:
Use File, it works with whitespaces:
String path = "file:///opt/storage/user-data/attachments/1_00100\\ 0042.jpg";
File f = new File(path);
如果您想用 %20 替换空格,请使用正则表达式:
If you want to replace spaces with %20 then use regex:
path.replaceAll("\\u0020", "%20");
这篇关于使用Java读取名称中有空格的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文