問題描述
我想從 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訪問文件名中帶有空格的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!