- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个在虚幻世界中生成随机植物的模组。我在游戏论坛上发布了该模组的初始版本,并且有人告诉我该模组的 Windows 版本存在问题。问题具体在于名称生成;它只是为每个名称返回 null。向我报告此问题的用户发布了此堆栈跟踪(相同的错误重复了多次;无需全部阅读):
这是发生错误的方法:
private static String getName(Random rn, boolean full) {
try {
String name;
int prefix = rn.nextInt(179);
int suffix = rn.nextInt(72);
Scanner sc1 = new Scanner(new File("srcwin\\Prefixes.txt")); // error occurs on this line
for (int i=0; i<prefix; i++) {
sc1.nextLine();
}
name = sc1.next();
if (name.contains("'")) {
name += " ";
}
if (full || rn.nextInt(3) == 0) {
if (rn.nextInt(3) == 0 && !name.contains(" ")) {
name += " ";
}
Scanner sc2 = new Scanner(new File("srcwin\\Suffixes.txt"));
for (int i=0; i<suffix; i++) {
sc2.nextLine();
}
name += sc2.next();
}
return name;
} catch (FileNotFoundException ex) {
Logger.getLogger(UrWPlantMod.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
该程序包含在包含目录 modwin、srcwin 和 META-INF 的 JAR 中。类文件包含在 modwin 中,而源代码和 txt 文件包含在 srcwin 中。报告此问题的用户运行的是 Windows 10 家庭版(版本 1511,内部版本 10586)。另一个运行 Windows 8.1 的用户(未给出其他具体信息)可以正常运行,没有 FileNotFoundExceptions 或 null 名称。
如果相关的话,我正在运行 Ubuntu 14.04,此代码中唯一的区别是文件路径是 src/Prefixes.txt
而不是 srcwin\\Prefixes.txt
,它对我来说运行得很好。
如果您想查看堆栈跟踪中提到的其他代码行:
berries[i] = new Plant(getName(rn, false) + "berry " + getBerryName(rn), rn.nextInt(4)+1, getImg(rn, "berry"));
和
createBerries(rn); // the above line of code is in the method called here
最佳答案
new Scanner(new File("srcwin\\Prefixes.txt"))
将从当前目录打开文件srcwin\Prefixes.txt
.
无法以这种方式访问 Jar 文件内的目录。
因此,要么当前目录不是您想象的那样,要么文件不存在(位于文件系统上的文件夹 srcwin
中)。
要加载 Jar 中的文件内容(我们假设位于类路径上),请使用 getResourceAsStream()
.
try (Scanner sc1 = new Scanner(MyClass.class.getResourceAsStream("/srcwin/Prefixes.txt"))) {
// code here
}
请注意文件名的更改。它以 /
开头并使用 /
。
另请注意,使用完后应始终关闭 Scanner
(除非与 System.in
一起使用),因此需要使用 try-with-resources block 。
关于java.io.FileNotFoundException 在一台计算机上,但在另一台计算机上没有 [长帖子],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36851547/
我在 VisualStudio2010 中创建了小的 Windows Forms progs,只是为了业余爱好。释放它们后,我使用 .exe 文件在其他 PC 上运行它们,而无需 进行任何安装。这些
我正在尝试使用AvroParquetWriter将Avro格式的文件转换为 Parquet 文件。我加载架构 val schema:org.apache.Schema = ... getSchema(
我正在尝试使用 Image.IO.Write() 保存图像;我基本上从 here 复制了标准代码使用 lwjgl 截取屏幕截图。我唯一做的就是使用现有目录作为保存路径来初始化文件。 当我尝试保存图像时
错误: E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/storage/sdcard0/
如果这是基本的,我很抱歉,我错过了一些简单的东西。我正在尝试运行下面的代码以遍历文件夹中的文件并将所有以特定字符串开头的文件合并到数据框中。所有文件都放在一个湖中。 file_list=[] path
在我的数据库中,我的手机上的每个条目都有一个图片的 uri。为了在手机上显示它,Listview 有一个 CustomAdapter。现在,我想在 ListView 中显示图片,得到如下错误信息: 0
我的所有节点在压缩期间都抛出 FileNotFoundException。因此,没有一个压缩(自动、手动)可以完成,我的 SSTable 计数现在是单个 CF (CQL3) 的数千个。 nodetoo
我在 java 中读取文件时遇到一些问题: 我的文件是例如: 3,4 2 6 4 1 7 3 8 9 其中第一行 3 和 4 是数组 A 和 B 的长度,然后是每个数组的元素。 我做的 import
我创建了一个程序,其中保存了学生的成绩,我想将这些成绩存储在txt文件中,然后在启动程序时,导入成绩,并在程序完成后导出成绩。我将import和exportTo方法放在单独的文件中,然后在主类中调用这
我怎样才能捕获一个 com.sun.faces.context.FacesFileNotFoundException 在 Java EE 网络应用程序中? 我尝试在我的 web.xml 文件中添加以下
请帮忙,我正在尝试从此谷歌翻译 API URL 获取数据仅当值为 1 个单词时它才有效。如果值为 2,则会出现错误。 我的意思是这个值会起作用: String sourceLang = "auto";
当我尝试使用retrofit2上传图片时,出现此错误 :java.io.FileNotFoundException(No such file or directory). HashMap partMa
try { FileReader fr = new FileReader("C:\\Users\\kevin\\Desktop\\AndroidLibr\\LeagueStats\\a
我尝试使用 Java 将单个文件从源复制到目标,但收到以下错误消息。 java.io.FileNotFoundException:以下是方法 public void copy_single(Strin
类似的问题涉及 C: 上的文件。驱动器,其中对文件路径进行硬编码是可接受的答案。此应用程序是移动应用程序,对文件路径进行硬编码并不实用。 我正在尝试通过扫描仪导入一个文本文件,其中包含一个字符串列表,
我正在修改一个小应用程序以从文件中读取一些数字。到目前为止一切都运行良好,但现在我遇到了一个问题,我不知道如何有效地解决它。如果用户输入了错误的文件名(可能是无意的),JVM 将抛出 FileNotF
我有一个 Web 项目,其中使用以下代码: try { br1 = new BufferedReader(new FileReader("queryWords.txt")); } catch
我尝试使用绝对路径从文件系统读取文件,但由于“FileNotFoundException”而失败,我不知道为什么 File file=new File("E:\\Directory\\File.txt
在我当前的项目中,我遇到了未收到文件未找到异常的问题。我的驱动程序文件将要打开的路径传递给正在构建图书库的构造函数。我正在使用 JFileChooser 来获取路径。在尝试强制错误(输入不存在的文件名
这个问题已经有答案了: Java: Unresolved compilation problem (10 个回答) 已关闭 4 年前。 我已经查看了有关此问题的其他答案,并尝试了他们的建议,但没有成功
我是一名优秀的程序员,十分优秀!