【发布时间】:2020-06-23 16:13:37
【问题描述】:
所以我有一个项目,这是需求之一:
You should have a class named Project3, containing a main method.
This program reads the levels information from a file whose name is
specified as a command-line parameter (The file should also be
relative to the class-path as described here:)
关卡和块定义文件中指定的所有文件名 应该相对于类路径。我们希望他们成为的原因 相对于类路径的是,稍后我们将能够读取 jar 中的文件,这是我们无法使用常规文件执行的操作 参考文献。
获取一个相对于类路径的输入流(即使它在里面 一个罐子),使用以下内容:
输入流是 = ClassLoader.getSystemClassLoader().getResourceAsStream("image.png"); 这个想法是保留一个包含文件(定义和图像)的文件夹和 然后在运行 JVM 时将该文件夹添加到类路径中:
java -cp bin:resources ... 如果不添加资源文件夹 您的类路径将无法使用以下命令加载它们 以上。
When run without parameters, your program should read a default level
file, and run the game accordingly. The location of the default level
file should be hard-coded in your code, and be relative to the
classpath_.
当不带参数运行时,你的程序应该读取一个默认的关卡文件,并相应地运行游戏。默认关卡文件的位置应该在你的代码中硬编码,并且是相对于classpath_的。
处理输入的部分代码是:
public Void run() throws IOException {
LevelReader level = new LevelReader();
List<level> chosenLevels = new ArrayList<>();
if (args.length >= 1) {
File f = new File(args[0]);
if (f.exists()) {
chosenLevels = level.makeLevel(args[0]);
}
}
if (chosenLevels.size() == 0) {
game.runLevels(defaultLevels);
} else {
game.runLevels(chosenLevels);
}
return null;
}
所以我的问题是:
-
参数应该是文件的完整路径,这意味着: D:\desktop\level3.txt
是否可以从我计算机上的每个位置读取文件? 因为现在我只能在我的文本文件位于 项目的目录(甚至不在 src 文件夹中)。
-
我无法理解他们的其他要求。什么是“应该在你的代码中硬编码,并且相对于 classpath_." 以及为什么它与 InputStream 方法有关(?)
我对此感到困惑。 谢谢。
【问题讨论】:
-
此网页可能会有所帮助:Accessing Resources