由于我不清楚您的输入文件和预期输出,所以我专注于阅读文件,我希望每行已经有 10 个字符串,并且有 10 行给你你的网格
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
class Scratch {
public static void main(String[] args) throws IOException {
List<String> allLinesInFile = Files.readAllLines(Paths.get("/tmp/maze.input"));
String[] asArray = allLinesInFile.toArray(new String[0]);
System.out.println(Arrays.toString(asArray));
}
}
更新:
既然现在输入和输出都清楚了,这里是完整的代码:)
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.*;
class Scratch {
public static void main(String[] args) throws IOException {
Path mazeInputFile = Paths.get("/tmp/maze.input");
Files.writeString(mazeInputFile, "2 3 3 0 0 0 1 0 0 3\n", StandardOpenOption.TRUNCATE_EXISTING);
Files.writeString(mazeInputFile, "3 0 0 0 0 0 0 3 0 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 0 0 3 3 0 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 0 0 0 0 0 0\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 0 0 3 3 3 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 0 3 3 3 3 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 3 3 3 3 3 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 3 3 3 3 3 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 0 0 3 3 3 3 3 3\n", StandardOpenOption.APPEND);
Files.writeString(mazeInputFile, "3 3 3 2 3 3 3 3 3 3\n", StandardOpenOption.APPEND);
System.out.printf("Wrote mazeInputFile = %s\n", mazeInputFile);
System.out.printf("Reading all Lines From Input file : %s\n", mazeInputFile);
String[][] mazeInputArray = Files.lines(mazeInputFile) // Gives The lines in file as a Stream
.filter(line -> line != null && !line.isBlank()) // Filter out unwanted Lines
.map(line -> line.split(" ", 10)) // Split the line by Space with a maximum of 10 columns
.toArray(String[][]::new); // Collect as a 2 dimensional Array
System.out.println("-- --");
System.out.println("-- Print the Array using a Java 8 Streams (my preference) --");
String printableMaze = java.util.Arrays.stream(mazeInputArray).
map(rows -> java.util.Arrays.stream(rows).
map(row -> String.join(",", row)). // join each row with a comma
collect(joining(",", "{", "}"))). // surround the row with a prefix { and suffix } and join the columns with a comma
collect(joining(",\n")); // join each row with a leading comma and a newline
System.out.printf("%s\n", printableMaze);
System.out.println("-- --");
System.out.println("-- Here, actually the reading of the file and printing can be combined to read huge files as well without taking too much memory --");
System.out.println("-- --");
System.out.println("-- Print the Array using good old for each loops, code looks kind of better but last comma remains --");
for (String[] rows : mazeInputArray) {
System.out.print("{");
System.out.print(String.join(",", rows));
System.out.print("},\n"); // needs ugly imperative code to get rid of the last comma :P
}
System.out.println("-- --");
}
}
输出看起来像
Wrote mazeInputFile = /tmp/maze.input
Reading all Lines From Input file : /tmp/maze.input
-- --
-- Print the Array using a Java 8 Streams (my preference) --
{2,3,3,0,0,0,1,0,0,3},
{3,0,0,0,0,0,0,3,0,3},
{3,3,0,0,0,0,3,3,0,3},
{3,3,0,0,0,0,0,0,0,0},
{3,3,0,0,0,0,3,3,3,3},
{3,3,0,0,0,3,3,3,3,3},
{3,3,0,0,3,3,3,3,3,3},
{3,3,0,0,3,3,3,3,3,3},
{3,3,0,0,3,3,3,3,3,3},
{3,3,3,2,3,3,3,3,3,3}
-- --
-- Here, actually the reading of the file and printing can be combined to read huge files as well without taking too much memory --
-- --
-- Print the Array using good old for each loops, code looks kind of better but last comma remains --
{2,3,3,0,0,0,1,0,0,3},
{3,0,0,0,0,0,0,3,0,3},
{3,3,0,0,0,0,3,3,0,3},
{3,3,0,0,0,0,0,0,0,0},
{3,3,0,0,0,0,3,3,3,3},
{3,3,0,0,0,3,3,3,3,3},
{3,3,0,0,3,3,3,3,3,3},
{3,3,0,0,3,3,3,3,3,3},
{3,3,0,0,3,3,3,3,3,3},
{3,3,3,2,3,3,3,3,3,3},
-- --
Process finished with exit code 0
- 我不确定您要做什么,但打印它与处理它不同。
- 我之所以将其分开,是因为您可以简单地使用
mazeInputArray 来对输入进行实际操作
- 打印是一种 PIA,使用流可以更轻松地不考虑边缘情况,因为它们是智能/隐式处理的。