从 Java 访问文件名中带有空格的文件 [英] Accessing files with spaces in filename from Java
问题描述
我想从 java 程序访问文件名中有空格的目录中的文件,但它不访问文件.
I want to access files in a directory which have spaces in filename from a java program but it doesnot access file.
场景是我在文件中有文件名.从该文件中读取文件名,但无法在 java 中打开带有空格的文件.
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 .
我们正在使用 File.exist 函数来检查文件是否存在,但它返回 false.
We are using File.exist function to check if file exist but it return false.
我尝试了几种格式来表示空格,例如ab c"表示文件名 ab c 和 ab%20c 表示同一文件.
i have tried several kind of formats to represent spaces llike "ab c"for file name ab c and ab%20c for same file.
但没有任何帮助.
推荐答案
这对我来说很好用.我根本没有逃避他们.
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")
这篇关于从 Java 访问文件名中带有空格的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!