[英]Reading from a file and storing into an array
我有一個我正在研究的項目是一個迷宮求解器,但我試圖找出從文件中讀取的內容並將其存儲到一個數組中。 我所查找的大多數東西,它們在程序中使用他們創建的數組,而我的需要我從文件中讀取並從那里構造它。 到目前為止,我有這個:
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();
}
當我使用 output 時,它會將整個數組排成一行,但我試圖獲得 map 格式,基本上是一個 10x10 的網格。 歡迎任何關於我如何解決這個問題的提示或建議。
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 想要的是
{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}
您好,我為您的問題寫了一個小例子。 希望它對你有用。
在 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
代碼:
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:
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 我不清楚,我專注於閱讀文件,我希望每行已經有 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("-- --");
}
}
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
來實際做一些輸入
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.