无效的文件路径,但仅当我使用字段构建文件名时

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

下面给我一个FileNotFoundException: Invalid file path

String fileName = "folder/file" + "." + this.ext;
try {
  File file = new File(fileName);
} catch(Exception e){
}

先前将this.ext设置为"txt"的位置

[玩耍之后,我发现这很好用。

String ext = "txt";
String fileName = "folder/file" + "." + ext;
try {
  File file = new File(fileName);
} catch(Exception e){
}

我为什么不能使用字段?

java file field filenotfoundexception
1个回答
0
投票

没有理由不能使用字段,即this.ext。如果将调试点放在下面的行中,则会发现this.ext未设置为"txt"

String fileName = "folder/file" + "." + this.ext;

如果您对调试器不满意,只需将以下行放在上面的行之前,就可以找到问题所在:

System.out.println("this.ext="+this.ext);
© www.soinside.com 2019 - 2024. All rights reserved.