java 文件流 空格,从java访问文件名中包含空格的文件

本文讨论了在Java中尝试访问包含空格的文件时遇到的问题,使用File.exists()函数检查文件存在但返回false的情况。作者提到尝试了多种格式化方式如使用转义字符和URL编码,但未成功。解决方案是不转义空格,确保使用正确的文件分隔符,例如使用System.getProperty("file.separator")获取操作系统适用的分隔符。
摘要由CSDN通过智能技术生成

I want to access files in a directory which have spaces in filename from a java program but it doesnot access file.

Scenario is i have names of file in a file . iread file names from that file and not able to open files with spaces in java .

We are using File.exist function to check if file exist but it return false.

i have tried several kind of formats to represent spaces llike "ab\ c"for file name ab c and ab%20c for same file.

but nothing is helping.

解决方案

This works fine for me. I do not escape them at all.

System.out.println(new File("/tmp/test 1/").exists());

Ensure you are useing the correct file separator for your operating system.

System.getProperty("file.separator")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值