如何将 txt 文件读入二维数组 Java
How to read a txt file into a 2d Array Java
快速提问,我如何将 txt 文件读入二维数组?我需要将每一行放入一个数组中,以便稍后使用它来测量长度和宽度的总和。这将用于生活游戏。
代码
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int matrix[][] = new int[size][size];
while (s.hasNext()) {
for (int i = 0; i < matrix.length; i++) {
for (int col = 0; col < matrix.length; col++) {
// matrix[i][col] = ;
}
}
}
s.close();
} catch (IOException i) {
System.out.println("Problems..");
}
}
文本文件
20 20
0 2
0 6
0 7
0 10
0 11
0 16
1 0
1 4
1 7
2 6
2 12
2 15
2 19
3 0
3 4
3 14
3 16
3 17
4 0
4 8
4 12
4 14
4 15
5 0
5 2
5 4
5 7
5 13
5 17
6 11
6 14
6 15
6 18
7 4
7 10
7 13
7 14
8 6
8 11
8 12
8 14
8 17
8 19
9 6
9 8
9 13
9 14
10 3
10 10
10 19
11 6
11 7
11 9
11 10
11 17
12 0
12 2
12 12
13 0
13 1
13 7
13 8
13 14
13 15
14 7
14 9
14 12
14 13
14 16
14 18
15 0
15 2
15 6
15 8
15 10
15 11
15 16
15 17
16 2
16 3
16 18
17 2
17 7
17 11
17 13
17 19
18 0
18 5
18 6
18 11
18 12
18 13
18 14
19 3
19 4
19 6
19 8
19 11
19 16
19 17
您对 int
值感兴趣,所以使用这个。
此外,hasNextLine
条件已移入 行 循环,并且 hasNextInt
条件已添加到 列循环。
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int matrix[][] = new int[size][size];
for (int i = 0; i < matrix.length && s.hasNextLine(); i++) {
for (int col = 0; col < matrix.length && s.hasNextInt(); col++) {
matrix[i][col] = s.nextInt() ;
}
s.nextLine(); // col values populated for this row, time to go to the next line
}
s.close();
} catch (IOException i) {
System.out.println("Problems..");
}
}
public int[][] P4_Icel_Murad_Life(String fileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
// n = Rownum ; m = Colnum
int array[][] = new int[n][m];
String line = reader.readLine();
int i = 0, j = 0;
while (line != null) {
String strArray[] = line.split(" ");
if (!line.trim().isEmpty()) {
for (String s : strArray) {
if (!s.trim().isEmpty()) {
array[i][j++] = Integer.parseInt(s);
}
}
line = reader.readLine();
i++;
j = 0;
}
}
reader.close();
return array;
} catch (IOException ex) {
System.out.println("Problems..");
}
return null;
}
你可以使用opencsv读写这样的文件
CSVReader reader = new CSVReader(
new FileReader("C:/Users/muro0/Desktop/life100.txt"), ' ');
// Read all rows at once
List<String[]> allRows = readAll(reader);
它将文件读取为字符串,但您当然可以将其转换为整数。
所以文本文件似乎包含生命游戏中生命在哪里的所有对。
您所做的是增加每次读取的 "i" 值,但这不是保证。
相反,以下协议似乎是文本文件之一。
- 行包含板的 X 和 Y 尺寸。
其余的线条包含有生命的地方。
所以你的代码应该看起来更像这样(除了你已经提供的之外不包括异常处理)
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int rows = s.nextInt();
int columns = s.nextInt();
int matrix[][] = new int[rows][columns];
while (s.hasNextInt()) {
int row = s.nextInt();
int col = s.nextInt(); // If this fails the text documnet is incorrect.
matrix[row][col] = 1;
}
s.close();
// At this point all dead cells are 0 and all live are 1
} catch (IOException i) {
System.out.println("Problems..");
}
}
快速提问,我如何将 txt 文件读入二维数组?我需要将每一行放入一个数组中,以便稍后使用它来测量长度和宽度的总和。这将用于生活游戏。
代码
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int matrix[][] = new int[size][size];
while (s.hasNext()) {
for (int i = 0; i < matrix.length; i++) {
for (int col = 0; col < matrix.length; col++) {
// matrix[i][col] = ;
}
}
}
s.close();
} catch (IOException i) {
System.out.println("Problems..");
}
}
文本文件
20 20
0 2
0 6
0 7
0 10
0 11
0 16
1 0
1 4
1 7
2 6
2 12
2 15
2 19
3 0
3 4
3 14
3 16
3 17
4 0
4 8
4 12
4 14
4 15
5 0
5 2
5 4
5 7
5 13
5 17
6 11
6 14
6 15
6 18
7 4
7 10
7 13
7 14
8 6
8 11
8 12
8 14
8 17
8 19
9 6
9 8
9 13
9 14
10 3
10 10
10 19
11 6
11 7
11 9
11 10
11 17
12 0
12 2
12 12
13 0
13 1
13 7
13 8
13 14
13 15
14 7
14 9
14 12
14 13
14 16
14 18
15 0
15 2
15 6
15 8
15 10
15 11
15 16
15 17
16 2
16 3
16 18
17 2
17 7
17 11
17 13
17 19
18 0
18 5
18 6
18 11
18 12
18 13
18 14
19 3
19 4
19 6
19 8
19 11
19 16
19 17
您对 int
值感兴趣,所以使用这个。
此外,hasNextLine
条件已移入 行 循环,并且 hasNextInt
条件已添加到 列循环。
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int matrix[][] = new int[size][size];
for (int i = 0; i < matrix.length && s.hasNextLine(); i++) {
for (int col = 0; col < matrix.length && s.hasNextInt(); col++) {
matrix[i][col] = s.nextInt() ;
}
s.nextLine(); // col values populated for this row, time to go to the next line
}
s.close();
} catch (IOException i) {
System.out.println("Problems..");
}
}
public int[][] P4_Icel_Murad_Life(String fileName) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
// n = Rownum ; m = Colnum
int array[][] = new int[n][m];
String line = reader.readLine();
int i = 0, j = 0;
while (line != null) {
String strArray[] = line.split(" ");
if (!line.trim().isEmpty()) {
for (String s : strArray) {
if (!s.trim().isEmpty()) {
array[i][j++] = Integer.parseInt(s);
}
}
line = reader.readLine();
i++;
j = 0;
}
}
reader.close();
return array;
} catch (IOException ex) {
System.out.println("Problems..");
}
return null;
}
你可以使用opencsv读写这样的文件
CSVReader reader = new CSVReader(
new FileReader("C:/Users/muro0/Desktop/life100.txt"), ' ');
// Read all rows at once
List<String[]> allRows = readAll(reader);
它将文件读取为字符串,但您当然可以将其转换为整数。
所以文本文件似乎包含生命游戏中生命在哪里的所有对。
您所做的是增加每次读取的 "i" 值,但这不是保证。
相反,以下协议似乎是文本文件之一。
- 行包含板的 X 和 Y 尺寸。
其余的线条包含有生命的地方。
所以你的代码应该看起来更像这样(除了你已经提供的之外不包括异常处理)
public P4_Icel_Murad_Life(String fileName) {
Scanner in;
try {
Scanner s = new Scanner(new File("C:/Users/muro0/Desktop/life100.txt"));
int rows = s.nextInt();
int columns = s.nextInt();
int matrix[][] = new int[rows][columns];
while (s.hasNextInt()) {
int row = s.nextInt();
int col = s.nextInt(); // If this fails the text documnet is incorrect.
matrix[row][col] = 1;
}
s.close();
// At this point all dead cells are 0 and all live are 1
} catch (IOException i) {
System.out.println("Problems..");
}
}