猿问

逐行读取文件从 txt 到整数数组

我有一个如下所示的文本文件:


1 2 3 4 5 8 

12 22 5 33 11 56

5 2 3 45 89 45

我包含了一个我尝试过但没有用的代码,因为每次当我尝试将行打印到控制台时,该行看起来都是空的。


我尝试使用 ArrayList 读取文件,但无法正常工作。


BufferedReader bufReader = new BufferedReader(new 

FileReader("file1.txt"));

    ArrayList<String> listOfLines = new ArrayList<>();

    String line = bufReader.readLine();

    while (line != null) {

      listOfLines.add(line);

      line = bufReader.readLine();

      System.out.println("full: " + line);

      System.out.print("0: " + line.charAt(0));    

    }

我想逐行从文件读取到数组,例如:[12][22][5][33][11][56]


开心每一天1111
浏览 149回答 3
3回答

慕姐8265434

您可以使用流:Files.lines(Paths.get("file.txt")) &nbsp;&nbsp;&nbsp;&nbsp;.map(line&nbsp;->&nbsp;Arrays.stream(line.split("&nbsp;")) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.map(Integer::valueOf) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList())) &nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toList())这将返回List<List<Integer>>所有元素均为整数的 a 。如果您只想打印每一行的所有元素,那么您可以将最后一行替换为.forEach(System.out::println).

BIG阳

试试这个代码final String dataFilePath = "C:\\Users\\Desktop\\txt.txt";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList<String> listOfLines = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;List listOfLinesint = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BufferedReader reader;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader = new BufferedReader(new FileReader(dataFilePath));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line = reader.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (line != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listOfLinesint.add(line);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; line = reader.readLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(listOfLinesint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

慕无忌1623718

如果您想将文件读入每行一个数组中,您可以使用以下代码:import java.io.File;import java.io.IOException;import java.nio.file.Files;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.stream.Stream;public class ReadIntArrayFromFile {&nbsp; &nbsp; public static void main(String[] args) throws IOException {&nbsp; &nbsp; &nbsp; &nbsp; //enter your path to the file here&nbsp; &nbsp; &nbsp; &nbsp; File file = new File("src/main/java/integer_array_from_file/file.txt");&nbsp; &nbsp; &nbsp; &nbsp; //read the lines from the file&nbsp; &nbsp; &nbsp; &nbsp; List<String> lines = Files.readAllLines(file.toPath());&nbsp; &nbsp; &nbsp; &nbsp; //create a list of arrays (one array per line)&nbsp; &nbsp; &nbsp; &nbsp; List<int[]> arrayForEachLine = new ArrayList<int[]>(lines.size());&nbsp; &nbsp; &nbsp; &nbsp; for (String line : lines) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //create the array from the line (one array for each line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] array = Stream.of(line.split(" ")).mapToInt(Integer::parseInt).toArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //add the array to the list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arrayForEachLine.add(array);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //print the arrays&nbsp; &nbsp; &nbsp; &nbsp; arrayForEachLine.stream().map(array -> Arrays.toString(array)).forEach(System.out::println);&nbsp; &nbsp; }}输出(对于您问题中的示例文件内容)是:[1, 2, 3, 4, 5, 8][12, 22, 5, 33, 11, 56][5, 2, 3, 45, 89, 45]
随时随地看视频慕课网APP

相关分类

Java
我要回答