亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

開心每一天1111 2023-05-10 17:37:22
我有一個如下所示的文本文件:1 2 3 4 5 8 12 22 5 33 11 565 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]
查看完整描述

3 回答

?
慕姐8265434

TA貢獻1813條經驗 獲得超2個贊

您可以使用流:

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

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


查看完整回答
反對 回復 2023-05-10
?
BIG陽

TA貢獻1859條經驗 獲得超6個贊

試試這個代碼


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();

                    }


查看完整回答
反對 回復 2023-05-10
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

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


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]


查看完整回答
反對 回復 2023-05-10
  • 3 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號