Java 将文本文件读入数组列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36440723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:53:47  来源:igfitidea点击:

Reading text file into arraylist

javaarraylist

提问by Audrey

I really new to Java so I'm having some trouble figuring this out. So basically I have a text file that looks like this:

我对 Java 真的很陌生,所以我在弄清楚这一点时遇到了一些麻烦。所以基本上我有一个看起来像这样的文本文件:

1:John:false  
2:Bob:false    
3:Audrey:false

How can I create an ArrayList from the text file for each line?

如何从文本文件中为每一行创建一个 ArrayList?

采纳答案by Priyansh Goel

Read from a file and add each line into arrayList. See the code for example.

从文件中读取并将每一行添加到 arrayList 中。例如,请参阅代码。

public static void main(String[] args) {

        ArrayList<String> arr = new ArrayList<String>();
        try (BufferedReader br = new BufferedReader(new FileReader(<your_file_path>)))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                arr.add(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 

    }

回答by Danny0317

While the answer above me works, here's another way to do it. Make sure to import java.util.Scanner.

虽然我上面的答案有效,但这是另一种方法。确保导入 java.util.Scanner。

public static void main(String[] args) {
     ArrayList<String> list = new ArrayList<String>();
     Scanner scan = new Scanner("YOURFILE.txt");
     while(scan.hasNextLine()){
         list.add(scan.nextLine());
     }
     scan.close();
}

回答by Toshan

This will be help to you.

这将对您有所帮助。

List<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new FileReader("list.txt"));
String line;
while ((line = reader.readLine()) != null) {
    list.add(line);
}
reader.close();

Then you can access those elements in the arraylist.

然后您可以访问数组列表中的这些元素。

回答by Hector

java 8 lets you do this

java 8 可以让你做到这一点

String fileName = "c://lines.txt";
        List<String> list = new ArrayList<>();

        try (Stream<String> stream = Files.lines(Paths.get(fileName))) {

            list = stream
                    .map(String::toUpperCase)
                    .collect(Collectors.toList());

        } catch (IOException e) {
            e.printStackTrace();
        }

        list.forEach(System.out::println);

回答by Vikas Suryawanshi

If you know how to read a file line by line, either by using Scanner or by using BufferedReader then reading a text file into ArrayList is not difficult for you. All you need to do is read each line and store that into ArrayList, as shown in following example:

如果您知道如何通过使用 Scanner 或使用 BufferedReader 逐行读取文件,那么将文本文件读入 ArrayList 对您来说并不困难。您需要做的就是读取每一行并将其存储到 ArrayList 中,如下例所示:

BufferedReader bufReader = new BufferedReader(new 
FileReader("file.txt"));
ArrayList<String> listOfLines = new ArrayList<>);
String line = bufReader.readLine(); while (line != null)
{
    listOfLines.add(line);
    line = bufReader.readLine();
} 
bufReader.close();

Just remember to close the BufferedReader once you are done to prevent resource leak, as you don't have try-with-resource statement

请记住在完成后关闭 BufferedReader 以防止资源泄漏,因为您没有 try-with-resource 语句