更新: 私はを使用して、「\ n」は APIで指定されたセーブデータ方式における多様各行によ。これは、readLineがファイル内の個々の行を正しく見つけ、その値を割り当てて、最後の行を読み終わった後に閉じることを意味します。しかし、私はまだこれはAPIで指定されているかnullであるレディング(割り当てる)txt.file値
...出力として "ヌル"(NullPointerExceptionが)取得:
(のreadLine)戻り値:
含むString任意の 行の終端文字を含まない行の内容、またはnullストリームの終わりには、 は
に達した場合
私はtxtファイルを読み込み、その行をint [] []配列に代入しようとしています。すべてのラインがint値を表し
0
0
-1
0
1
-1
1
0
0
:
これは私の savegame.txtファイルがどのように見えるかの例です。私のsaveGameメソッドは、配列 "gameBoard"の現在のint値を上記のファイルに書き込みます。私はそれぞれを割り当て、私のloadGame方法、とsavegame.txtファイルを読み込むしようとすると、しかし
| | O
| X | O
X | |
:この例では、次のゲームの状態を表し、個々のgameBoard-position [0] [0]〜[2] [2]それぞれの値は、nullと出力され、ゲームはempで始まります配列。私のロジックから、私のloadGameメソッドは、すべての行を別々に読み込み、文字列値を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)
ファイルを読み込んでいる場合、なぜスキャナ(System.in)を使用していますか? – SomeJavaGuy
'System.out.println(e.getMessage());'の代わりに 'e.printStackTrace();'を試してください。これは、コンソールに例外表示に関する良い習慣であり、おそらくあなたを助けるでしょう。 – Paul
ああ、私はスキャナを使用できると思った... BufferedReaderを使用する必要があるのですか?https://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html – Dave