java - Invalid file path, but only if I build the file name with a field
问题描述
The following gives me a FileNotFoundException: Invalid file path
String fileName = "folder/file" + "." + this.ext;
try {
File file = new File(fileName);
} catch(Exception e){
}
Where this.ext has previously been set to "txt"
After playing around, I find that this works perfectly fine.
String ext = "txt";
String fileName = "folder/file" + "." + ext;
try {
File file = new File(fileName);
} catch(Exception e){
}
Why can't I use a field?
解决方案
没有理由不能使用字段 ie this.ext
。如果你在下面一行放了一个调试点,你会发现this.ext
没有设置为"txt"
String fileName = "folder/file" + "." + this.ext;
如果您对调试器不满意,只需将以下行放在上述行之前,您就可以找到问题所在:
System.out.println("this.ext="+this.ext);
推荐阅读
- c++ - 提升排序如何处理额外的内存分配=
- python - 如何提高我的 python 函数 for 循环的性能?
- swift - 使用未声明的标识符“_AppsFlyerdelegate”
- marshmallow - Marshmallow 和 Flask-RESTPlus,它们应该如何一起使用?
- algorithm - 重新排列字符串排列的算法。我该如何解决?
- python - 在 docker 的共享卷中创建符号并在主机中访问它
- pine-script - Pine Script 语言的非常奇怪的行为?
- javascript - 在 d3.js 热图中绘制节点 - 需要指导
- node.js - Redis 不断抛出内存不足 (OOM) 错误
- javascript - 使用正则表达式创建搜索功能 - JS、mongoDB