Java读取文件,其路径中包含空格 [英] Java read file with whitespace in its path
问题描述
我正在尝试使用名称中包含空格的FileInputStream打开文件。
I am trying to open files with FileInputStream that have whitespaces in their names.
例如:
String fileName = "This is my file.txt";
String path = "/home/myUsername/folder/";
String filePath = path + filename;
f = new BufferedInputStream(new FileInputStream(filePath));
结果是抛出了FileNotFoundException。
我试图将filePath硬编码为/ home / myUserName / folder / This\\ is\\ my\\ file.txt
只是为了看看我是否应该逃脱空白字符,它似乎没有用。
有关此问题的任何建议吗?
The result is that a FileNotFoundException is being thrown.
I tried to hardcode the filePath to "/home/myUserName/folder/This\\ is\\ my\\ file.txt"
just to see if i should escape whitespace characters and it did not seem to work.
Any suggestions on this matter?
编辑:只是与查看此问题的所有人在同一页面上...打开名称中没有空格的文件工作,有空格失败的工作。权限不是问题,也不是文件夹分隔符。
Just to be on the same page with everyone viewing this question...opening a file without whitespace in its name works, one that has whitespaces fails. Permissions are not the issue here nor the folder separator.
推荐答案
带空格的文件名工作正常
File name with space works just fine
这是我的代码
File f = new File("/Windows/F/Programming/Projects/NetBeans/TestApplications/database prop.properties");
System.out.println(f.exists());
try
{
FileInputStream stream = new FileInputStream(f);
}
catch (FileNotFoundException ex)
{
System.out.println(ex.getMessage());
}
f.exists()
返回 true
总是没有任何问题
f.exists()
returns true
always without any problem
这篇关于Java读取文件,其路径中包含空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!