Java FileNotFoundExeption,路径无效?
你好,我有个问题,已经花了我几个小时了, 但我无法解决它。 我的代码:Java FileNotFoundExeption,路径无效?,java,Java,你好,我有个问题,已经花了我几个小时了, 但我无法解决它。 我的代码: public static void main(String[] args) { FileTester ft = new FileTester(); boolean flag = false; try { flag = ft.checkFile("configuration/config.txt"); } catch (FileNotFoundException e) {
public static void main(String[] args) {
FileTester ft = new FileTester();
boolean flag = false;
try {
flag = ft.checkFile("configuration/config.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(!flag){
JOptionPane.showMessageDialog(null, "please edit configuration/config.txt to continue!");
System.exit(0);
}
UserInterface ui = new UserInterface();
ui.setVisible(true);
}
public class FileTester{
public boolean checkFile(String file) throws FileNotFoundException{
String zeile = null;
String path = getDirectory();
int j = 0;
for(int i = 0; i < path.length(); i++){
char c = path.charAt(i);
if(c == '.'){
j = i;
break;
}
}
path = path.substring(0, j-6);
System.out.println(path);
file = path + file;
System.out.println(file);
int i = 0;
BufferedReader in = new BufferedReader(new FileReader(file));
try {
while ((zeile = in.readLine()) != null) {
if(zeile.startsWith("#")){
}else{
i++;
System.out.println(zeile);
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
if(i >= 1){
return true;
}
return false;
}
String getDirectory(){
try {
return String.valueOf(MainClass.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
} catch (URISyntaxException e) {
e.printStackTrace();
return "false";
}
}
}
然后抛出一个FileNotFoundExeption
有人有好主意吗?我自己解决了,错误如下: 我的程序试图打开config.txt,但文件名是“config.txt”。
因此,程序试图打开“config.txt.txt”,这很愚蠢。您尝试过使用文件完整路径吗?我不知道这个问题,但您的代码是“配置”文件夹,然后您谈论“配置”文件夹(带“s”),然后在绝对路径中,它是“配置”,没有“s”。对于这类问题,你应该更准确地回答。@Unda抱歉,这篇文章中只是一个书写错误,文件夹的正确名称是“configuration”@Vitor Martins,是的
Windows:
C:\...\Desktop\smartC\configuration\config.txt