我正试图解决这个问题。搜索时由于文件名中的下划线,我无法找到该文件
File file = new File(filePath + "file_2.exe");
if (file.exists()){
System.out.println("File found");
}else{
System.out.println("File not found");
}
但我需要留下它,请问任何想法?
谢谢你的帮助提前。 :)
尝试使用new File(filePath, "file_2.exe")
- 注意两个参数和没有+
。
也许你只是在filePath
结束时错过反斜杠。
提供filePath变量的值和该程序的输出。 filePath可能有问题,因为反斜杠而不是正斜杠。
注意粗体中的行中的错误,'+'替换为','文件文件=新文件(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");
}
}
}