我有以下代码结构:
Projects/
classes/
performance/AcPerformance.class
resources/
Aircraft/
allAircraft.txt
Run Code Online (Sandbox Code Playgroud)
我在JAR中有classes文件夹的内容,我的AcPerformance scala代码试图读取Aircraft文件夹文本文件的内容.我的代码:
val AircraftPerf = getClass.getResource("resources/Aircraft").getFile
val dataDir = new File(AircraftPerf)
val acFile = new File(dataDir, "allAircraft.txt")
for (line <- linesFromResource(acFile)) {
// read in lines
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行代码时,我收到以下错误:
引起:java.io.FileNotFoundException:C:\ Projects\file:\ C:\ Projects\libraries\aircraft.jar!\ Aircraft\allAircraft.txt(文件名,目录名或卷标语法不正确)
这是读取JAR内容的正确方法吗?谢谢!
Tra*_*own 10
不,URL这getFile不会在这里做你想要的 - 它给你的路径不是你可以在File构造函数中使用的文件系统路径.你最好使用getResourceAsStream和资源的完整路径:
val in = getClass.getResourceAsStream("/resources/Aircraft/allAircraft.txt")
Run Code Online (Sandbox Code Playgroud)
请注意,您还需要在路径前加上/绝对值 - 在当前版本中,您正在寻找resources目录下的目录performance.
| 归档时间: |
|
| 查看次数: |
5270 次 |
| 最近记录: |