Java文件存在下划线

问题描述 投票:0回答:3

我正试图解决这个问题。搜索时由于文件名中的下划线,我无法找到该文件

    File file = new File(filePath + "file_2.exe");

    if (file.exists()){
        System.out.println("File found");
    }else{
        System.out.println("File not found");
    }

但我需要留下它,请问任何想法?

谢谢你的帮助提前。 :)

java file path exists
3个回答
0
投票

尝试使用new File(filePath, "file_2.exe") - 注意两个参数和没有+

也许你只是在filePath结束时错过反斜杠。


0
投票

提供filePath变量的值和该程序的输出。 filePath可能有问题,因为反斜杠而不是正斜杠。


0
投票

注意粗体中的行中的错误,'+'替换为','文件文件=新文件(filePath,“file_2.txt”);

包Stackoverflow;

import java.io.File;

public class FileUnderscore {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String filePath = "C:\\Users\\Pravin";
        File file = new File(filePath, "file_2.txt");

        if (file.exists()){
            System.out.println("File found");
        }else{
            System.out.println("File not found");
        }
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.