[英]Data reading in a jar File (in Java) and counting files
So here is my problem (I read the other answers, but didn't quite get it). 所以这是我的问题(我读了其他答案,但不太了解)。
In a group of 4, we have created a game in Java as a University Project. 在4人一组中,我们用Java作为大学项目创建了一个游戏。 Part of this is creating a *.jar File via Ant.
其中一部分是通过Ant创建* .jar文件。 There is several GameBoards saved in GameBoardx.txt Data where x is the number.
GameBoardx.txt数据中保存了多个GameBoard,其中x是数字。 We want to randomly select one of those.
我们要随机选择其中之一。 Therefore, every time a GameBoard is loaded, the files in the GameBoard directory are counted in order to generate a random number in the correct range.
因此,每次加载GameBoard时,都会对GameBoard目录中的文件进行计数,以生成正确范围内的随机数。 Our code works perfectly fine when running it from Eclipse.
从Eclipse运行时,我们的代码工作得很好。 It fails to run from the *.jar File and exits with a NullPointerException.
它无法从* .jar文件运行,并以NullPointerException退出。
int number = 0;
int fileCount = new File(new File("").getAbsolutePath()+"/GameBoards/").listFiles().length;
Random rand = new Random();
number = rand.nextInt(fileCount);
These Files are read later on using this: 这些文件将在以后使用此文件进行阅读:
static String fileName = new File("").getAbsolutePath();
static String line = null;
boolean verticalObstacles[][] = new boolean[16][17];
int currentLine = 1;
try {
FileReader fileReader = new FileReader(fileName+"/GameBoards/Board"+boardNumber+".txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null){
if (currentLine <17){
for (int i=0; i<17; i++){
if (line.charAt(i) == '1'){
verticalObstacles[currentLine-1][i] = true;
} else {
verticalObstacles[currentLine-1][i] = false;
}
}
}
currentLine ++;
}
bufferedReader.close();
The rest of the code works with the *.jar File and the *.txt Files are included in it. 其余代码与* .jar文件配合使用,并且其中包含* .txt文件。
The solutions I found were not good for us, because the code has to work with the *.jar File as well as just starting it from Eclipse to pass the test. 我发现的解决方案对我们不利,因为代码必须与* .jar文件配合使用,并且只能从Eclipse启动以通过测试。
What's the solution here to make in work in both? 在这两者中都可以工作的解决方案是什么?
Problem here is you can not read content of a Jar using File
, you shall use java.nio
classes to deal with this. 这里的问题是您不能使用
File
读取Jar的内容,您应该使用java.nio
类来处理此问题。
First of all you can read/get count of files from Jar/normal folder by using FileSystem
, Path
and FileVisitor
classes: 首先,您可以使用
FileSystem
, Path
和FileVisitor
类从Jar /普通文件夹中读取/获取文件计数:
Following code will work for both jar
as well as IDE
以下代码将同时适用于
jar
和IDE
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URI uri = sysClassLoader.getResource("GameBoards").toURI();
Path gameBoardPath = null;
if (uri.getScheme().equals("jar")) {
FileSystem fileSystem = FileSystems.newFileSystem(uri,
Collections.<String, Object> emptyMap());
gameBoardPath = fileSystem.getPath("/GameBoards");
} else {
gameBoardPath = Paths.get(uri);
}
PathVisitor pathVistor = new PathVisitor();
Files.walkFileTree(gameBoardPath, pathVistor);
System.out.println(pathVistor.getFileCount());
Following is the code for PathVisitor
class 以下是
PathVisitor
类的代码
class PathVisitor extends SimpleFileVisitor<Path> {
private int fileCount = 0;
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
fileCount++;
return FileVisitResult.CONTINUE;
}
public int getFileCount() {
return fileCount;
}
}
And then you shall read content of specific file by using ClassLoader#getResourceAsStream
然后,您应该使用
ClassLoader#getResourceAsStream
读取特定文件的内容
// ADD your random file picking logic here based on file Count to get boardNum
int boardNum = 1;
InputStream is = sysClassLoader.getResourceAsStream("GameBoards/Board" + boardNum + ".txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while((line=reader.readLine())!=null) {
System.out.println(line);
}
Hope this resolves your concerns and helps you in right direction. 希望这能解决您的疑虑并为您提供正确的指导。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.