2016-05-19 27 views
0

更新: 我使用「\ N」在遊戲存檔方法,它是在API中規定的不同的每一行。這意味着readLine應正確找到文件中的每一行,分配它的值並在讀取最後一行後關閉。讀數(分配)在Java中txt.file值

包含字符串:不過,我仍然得到 「空」(空指針異常)作爲輸出...

這是怎麼空的API中規定:

(的readLine)返回行的內容,不包括任何 行終止符,或NULL,如果流的末尾,一直達到


我想讀取一個txt文件並將其行分配到一個int [] []數組。

這是文件如何我savegame.txt看起來像一個例子:

0 

0 

-1 

0 

1 

-1 

1 

0 

0 

每一行代表一個INT-值。我的saveGame方法將數組「gameBoard」的當前int值寫入上面顯示的文件中。這個例子代表了以下游戲狀態:

| | O 

| X | O 

X | | 

但是,當我嘗試閱讀我的loadGame方法,其中分配每個savegame.txt文件個人gameBoard位置[0] [0]至[2] [2]其各自的價值觀,我得到null作爲輸出和遊戲以emp開始ty數組。從我的邏輯來看,我的loadGame方法應該單獨讀取每一行,並將其String-value解析爲一個Integer,它可以被我的int [] []數組gameBoard解釋。 我想知道爲什麼這不能正常工作。

FileManagement.class

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

的readLine API-文檔

public String readLine() 
       throws IOException 

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. 

Returns: 
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached 

Throws: 
IOException - If an I/O error occurs 

See Also: 
Files.readAllLines(java.nio.file.Path, java.nio.charset.Charset) 
+2

如果您正在從文件中讀取數據,您爲什麼使用'Scanner(System.in)'? – SomeJavaGuy

+0

嘗試'e.printStackTrace();'而不是'System.out.println(e.getMessage());'。這對於控制檯中的異常顯示是很好的做法,很可能會對你有所幫助。 – Paul

+0

哦,我以爲我可以使用掃描儀......做我必須使用的BufferedReader的https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html – Dave

回答

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

此代碼可能適用於您。

+0

感謝您的迴應。 – Dave

+0

@Dave一開始答案被接受..現在不是。?有什麼不對的嗎? –

+0

我仍然得到NullPointerException,但我會重新標記你的答案。謝謝你的努力。 – Dave