簡體   English   中英

將文件從txt逐行讀取到整數數組

[英]Read file from txt to integer array line by line

我有一個看起來像這樣的文本文件:

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]

您可以使用流:

Files.lines(Paths.get("file.txt"))
    .map(line -> Arrays.stream(line.split(" "))
        .map(Integer::valueOf)
        .collect(Collectors.toList()))
    .collect(Collectors.toList())

這將返回一個List<List<Integer>> ,所有元素均為整數。 如果只想打印每一行的所有元素,則可以用.forEach(System.out::println)替換最后一行。

如果要將文件每行讀入一個數組,可以使用以下代碼:

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 {

    public static void main(String[] args) throws IOException {
        //enter your path to the file here
        File file = new File("src/main/java/integer_array_from_file/file.txt");
        //read the lines from the file
        List<String> lines = Files.readAllLines(file.toPath());

        //create a list of arrays (one array per line)
        List<int[]> arrayForEachLine = new ArrayList<int[]>(lines.size());
        for (String line : lines) {
            //create the array from the line (one array for each line)
            int[] array = Stream.of(line.split(" ")).mapToInt(Integer::parseInt).toArray();
            //add the array to the list
            arrayForEachLine.add(array);
        }

        //print the arrays
        arrayForEachLine.stream().map(array -> Arrays.toString(array)).forEach(System.out::println);
    }
}

輸出(對於您問題中的示例文件內容)為:

[1, 2, 3, 4, 5, 8]
[12, 22, 5, 33, 11, 56]
[5, 2, 3, 45, 89, 45]

試試這個代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM