java读取文件空格,Java读取文件,其路径中包含空格

I am trying to open files with FileInputStream that have whitespaces in their names.

For example:

String fileName = "This is my file.txt";

String path = "/home/myUsername/folder/";

String filePath = path + filename;

f = new BufferedInputStream(new FileInputStream(filePath));

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?

EDIT: 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

Here is my code

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() returns true always without any problem

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值