2016-04-26 25 views
0

これは私の問題です(私は他の答えを読んでいますが、それを得られませんでした)。jarファイルのデータ読み込み(Javaの場合)

4人で、大学プロジェクトとしてJavaでゲームを作成しました。これの一部は、Antを介して* .jarファイルを作成することです。 GameBoardx.txtデータにはいくつかのGameBoardが保存されています。ここで、xは数字です。そのうちの1つをランダムに選択したい。したがって、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から起動する必要があるため、私たちにとっては良くありませんでした。

ここで解決する方法はどちらですか?

+0

GameBoardsディレクトリには、jar以外のファイルを保存してください。 – Sanjeev

+0

これは適用されません。 jarファイルを作成するように求められている理由は、追加の作業をせずに人が送信して使用できる単一のファイルがあるからです。 – Shadol

答えて

1

ここでは、Fileを使用してJarの内容を読み取ることができない場合、これに対処するにはjava.nioクラスを使用します。

あなたがFileSystemPathFileVisitorクラスを使用してJAR /通常のフォルダからのファイルの数を取得する/読むことができますまず第一に:次のコード

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()); 
の両方のために動作します

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

を使用して、特定のファイルの内容を読み取るもので、これはあなたの懸念を解決し、正しい方向にあなたを助け願っています。

+0

うわー、これは素晴らしいことですし、多くの助けになります!今それは動作します。ありがとうございました。あなたの助けは本当に高く評価されています:) – Shadol

+0

喜んでそれはあなたを助けた..歓声 – Sanjeev

関連する問題