在 Java 中读取(分配)txt.file 个值
Reading (assigning) txt.file values in Java
更新:
我使用 '\n' 来改变 saveGame 方法中的每一行,这是在 API 中指定的。这意味着 readLine 应该正确地找到文件中的每一行,分配它的值并在读取最后一行后关闭。但是,我仍然得到 "null" (NullPointerException) 作为输出...
这就是在 API 中指定 null 的方式:
(readLine) 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
我正在尝试读取一个 txt 文件并将其行分配到一个 int [][] 数组中。
这是我的 savegame.txt 文件的示例:
0
0
-1
0
1
-1
1
0
0
每一行代表一个整数值。我的 saveGame 方法将数组 "gameBoard" 的当前 int 值写入上面显示的文件中。此示例表示以下游戏状态:
| | O
| X | O
X | |
但是,当我尝试使用我的 loadGame 方法读取 savegame.txt 文件时,该方法将每个单独的游戏板位置 [0][0] 分配给 [2][2] 各自的值,我得到null 作为输出,游戏以空数组开始。根据我的逻辑,我的 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)
请更改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());
}
此代码可能适合您。
更新: 我使用 '\n' 来改变 saveGame 方法中的每一行,这是在 API 中指定的。这意味着 readLine 应该正确地找到文件中的每一行,分配它的值并在读取最后一行后关闭。但是,我仍然得到 "null" (NullPointerException) 作为输出...
这就是在 API 中指定 null 的方式:
(readLine) 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
我正在尝试读取一个 txt 文件并将其行分配到一个 int [][] 数组中。
这是我的 savegame.txt 文件的示例:
0
0
-1
0
1
-1
1
0
0
每一行代表一个整数值。我的 saveGame 方法将数组 "gameBoard" 的当前 int 值写入上面显示的文件中。此示例表示以下游戏状态:
| | O
| X | O
X | |
但是,当我尝试使用我的 loadGame 方法读取 savegame.txt 文件时,该方法将每个单独的游戏板位置 [0][0] 分配给 [2][2] 各自的值,我得到null 作为输出,游戏以空数组开始。根据我的逻辑,我的 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)
请更改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());
}
此代码可能适合您。