[英]Reading from a file and storing into an array
I have a project that I am working on that is a maze solver, but I'm trying to figure out the read in from a file and storing it into an array.我有一个我正在研究的项目是一个迷宫求解器,但我试图找出从文件中读取的内容并将其存储到一个数组中。 Most of the stuff that I have looked up to, they work with a created array by them in the program, while mine requires me to read from a file and construct it from there.我所查找的大多数东西,它们在程序中使用他们创建的数组,而我的需要我从文件中读取并从那里构造它。 So far, I have this:到目前为止,我有这个:
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();
}
When I output it, it throws me a whole array in a line, but I am trying to get in a map format basically a grid 10x10.当我使用 output 时,它会将整个数组排成一行,但我试图获得 map 格式,基本上是一个 10x10 的网格。 Any tips or any advice on how I can approach this problem are welcome.欢迎任何关于我如何解决这个问题的提示或建议。
output as of right now [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] output 目前 [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]
output wanted is output 想要的是
{2, 3, 3, 0, 0, 0, 1, 0, 0, 3}, {2, 3, 3, 0, 0, 0, 1, 0, 0, 3},
{3, 0, 0, 0, 0, 0, 0, 3, 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, 3, 3, 0, 3},
{3, 3, 0, 0, 0, 0, 0, 0, 0, 0}, {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, 0, 3, 3, 3, 3},
{3, 3, 0, 0, 0, 3, 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, 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} {3, 3, 3, 2, 3, 3, 3, 3, 3, 3}
Hello I wrote a small example for your problem.您好,我为您的问题写了一个小例子。 Hope it will be useful for you.希望它对你有用。
In map.txt在 map.txt
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
Code:代码:
public static void main(String[] args) {
final int mapSize = 10;
List<List<String>> map = new ArrayList<>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("F:\\map.txt"));
String line = reader.readLine();
if(line != null && !line.isEmpty()) {
for (int i = 0; i < mapSize; i++) {
map.add(i, new ArrayList<>());
String substring = line.trim().substring(0, mapSize * 2 - 1); // 2 for whitespaces
line = line.trim().substring(mapSize * 2 - 1);
map.get(i).addAll(Arrays.asList(substring.split(" ")));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
for(List<String> row : map) {
System.out.println(String.join(",", row));
}
}
Output: Output:
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
Since your input file and expected output was not clear to me, I concentrate on reading the file, I am hoping each line already has 10 strings and has 10 lines to give you your grid由于您的输入文件和预期的 output 我不清楚,我专注于阅读文件,我希望每行已经有 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));
}
}
Update: Since now the inputs and outputs are clear, here is the full code:)更新:由于现在输入和输出很清楚,这里是完整的代码:)
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("-- --");
}
}
The Output looks like Output 看起来像
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 actually doing something with the input为什么我把它分开是因为你可以简单地使用mazeInputArray
来实际做一些输入
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.