簡體   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