在Java中读取(分配)txt.file值

时间:2016-05-19 11:55:47

标签: java arrays file java-io

更新 我正在使用'\ n'来分隔saveGame方法中的每一行,该方法在API中指定。这意味着readLine应正确查找文件中的每一行,分配其值并在读取最后一行后关闭。但是,我仍然将“null”(NullPointerException)作为输出...

这是在API中指定null的方式:

  

(readLine)返回:

     

包含该行内容的String,不包括任何内容   行终止字符,如果流结束,则为null   达到

我正在尝试读取txt文件并将其行分配到int [] []数组中。

这是我的 savegame.txt 文件的示例:

finaldir/

每一行代表一个int值。我的saveGame方法将数组“gameBoard”的当前int值写入上面显示的文件中。此示例表示以下游戏状态:

0 

0

-1

0

1

-1

1

0

0
  |   | O 
  | X | O

但是,当我尝试使用我的loadGame方法读取savegame.txt文件时,它将每个gameBoard-position [0] [0]分配给[2] [2]各自的值,我得 null 作为输出,游戏以空数组开始。从我的逻辑来看,我的loadGame方法应该单独读取每一行并将其String值解析为一个Integer,而我的int [] []数组gameBoard可以解释它。 我想知道为什么这不能正常工作。

FileManagement.class

X |   |  

readLine API-Documentation

package storagePack;

import structurePack.Board;

import java.io.*;
import java.util.Scanner;

/**
 * The FileManagement-Class is responsible for the saving and loading of gamedata.
 * It saves the current gameBoard and reads it with the loadGame Method.
 */

public class FileManagement {
    static String filename = "Savegame.txt";

/**
 * Schema:
 * (0,0) | (0,1) | (0,2)
 * (1,0) | (1,1) | (1,2)
 * (2,0) | (2,1) | (2,2)
 */
/**
 * @param gameBoard, the currently active Array from the Board.class.
 * @throws Exception, FileNotFoundException
 */
public static void saveGame(int[][] gameBoard) throws Exception {
    //serialization
    PrintWriter writer;
    try {

        writer = new PrintWriter(new FileOutputStream(filename), false);
        for (int i = 0; i < gameBoard.length; i++) {
            for (int j = 0; j < gameBoard.length; j++) {
                int entry = gameBoard[i][j];
                writer.print(entry);
                writer.println('\n');
            }

        }
        writer.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}

/**
 * @throws Exception, FileNotFoundException
 */
public static void loadGame() throws Exception {
    //deserialization
    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader (fileReader);
    try {
        structurePack.Board.gameBoard[0][0] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[0][1] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[0][2] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[1][0] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[1][1] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[1][2] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[2][0] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[2][1] = Integer.parseInt(bufferedReader.readLine());
        structurePack.Board.gameBoard[2][2] = Integer.parseInt(bufferedReader.readLine());
        fileReader.close();
        bufferedReader.close();

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
}
}

1 个答案:

答案 0 :(得分:0)

请更改loadGame方法,如下所示:

public static void loadGame() throws Exception {
    //deserialization

    FileReader fileReader = new FileReader(filename);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    structurePack.Board.gameBoard[0][0] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[0][1] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[0][2] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[1][0] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[1][1] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[1][2] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[2][0] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[2][1] = Integer.parseInt(bufferedReader.readLine());
    structurePack.Board.gameBoard[2][2] = Integer.parseInt(bufferedReader.readLine());
}

此代码可能适合您。