繁体   English   中英

在jar文件(Java)中读取数据并计数文件

[英]Data reading in a jar File (in Java) and counting files

所以这是我的问题(我读了其​​他答案,但不太了解)。

在4人一组中,我们用Java作为大学项目创建了一个游戏。 其中一部分是通过Ant创建* .jar文件。 GameBoardx.txt数据中保存了多个GameBoard,其中x是数字。 我们要随机选择其中之一。 因此,每次加载GameBoard时,都会对GameBoard目录中的文件进行计数,以生成正确范围内的随机数。 从Eclipse运行时,我们的代码工作得很好。 它无法从* .jar文件运行,并以NullPointerException退出。

int number = 0;
int fileCount = new File(new File("").getAbsolutePath()+"/GameBoards/").listFiles().length;
Random rand = new Random();
number = rand.nextInt(fileCount);

这些文件将在以后使用此文件进行阅读:

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();

其余代码与* .jar文件配合使用,并且其中包含* .txt文件。

我发现的解决方案对我们不利,因为代码必须与* .jar文件配合使用,并且只能从Eclipse启动以通过测试。

在这两者中都可以工作的解决方案是什么?

这里的问题是您不能使用File读取Jar的内容,您应该使用java.nio类来处理此问题。

首先,您可以使用FileSystemPathFileVisitor类从Jar /普通文件夹中读取/获取文件计数:

以下代码将同时适用于jarIDE

    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());

以下是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;
    }
}

然后,您应该使用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);
    }

希望这能解决您的疑虑并为您提供正确的指导。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM