java - 从文件中读取并存储到数组中
问题描述
我有一个我正在研究的项目是一个迷宫求解器,但我试图找出从文件中读取的内容并将其存储到一个数组中。我所查找的大多数东西,它们在程序中使用他们创建的数组,而我的需要我从文件中读取并从那里构造它。到目前为止,我有这个:
File map = new File("/Users/michelmaza/Downloads/Project1Map.txt"); //create an object of the file we are reading in
Scanner scanner = new Scanner(map); //create scanner to read file in
LinkedList<String> mapping = new LinkedList<String>();//create array list to store the reading from the file
while (scanner.hasNextLine()) {
mapping.add(scanner.nextLine()); //Stores the file into the string where every 10 number is a new line
}
System.out.println(mapping); //test to see if it works.
scanner.close(); //close the scanner since its not used anymore.
}
catch (FileNotFoundException e) { //catch in case the file is not found.
System.out.println("There was no file found, please try again.");
e.printStackTrace();
}
当我输出它时,它会将整个数组排成一行,但我试图获得基本上是 10x10 网格的地图格式。欢迎任何关于我如何解决这个问题的提示或建议。
截至目前的输出 [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]
想要的输出是
{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}
解决方案
由于我不清楚您的输入文件和预期输出,我专注于阅读文件,我希望每行已经有 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
for 实际对输入进行操作 - 打印是一种 PIA,使用流可以更容易地不考虑边缘情况,因为它们是智能/隐式处理的。