【问题标题】:Data reading in a jar File (in Java) and counting files在 jar 文件(Java 中)中读取数据并计数文件
【发布时间】:2016-08-20 12:27:02
【问题描述】:

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

在一个 4 人小组中,我们用 Java 创建了一个游戏作为大学项目。其中一部分是通过 Ant 创建一个 *.jar 文件。 GameBoardx.txt 数据中保存了几个游戏板,其中 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 启动它才能通过测试。

在这两种情况下都有什么解决方案?

【问题讨论】:

  • 您应该将您的 GameBoards 目录与 jar 之外的文件一起保存
  • 那不适用。我们被要求创建 jar 文件的原因是为了让我们有一个可以发送并供人们使用的单个文件,而无需任何额外的工作。

标签: java file jar directory


【解决方案1】:

这里的问题是你不能使用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);
    }

希望这能解决您的疑虑并帮助您朝着正确的方向前进。

【讨论】:

  • 哇,这太棒了,帮助很大!现在可以了。非常感谢。非常感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
相关资源
最近更新 更多