Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,700 questions

57,013 answers

573 users

How to read lines from text file into ArrayList in Java

1 Answer

0 votes
package javaapplication1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class JavaApplication1 {

    public static void main(String[] args) {

        try {

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

            try (BufferedReader reader = new BufferedReader(new FileReader("d:\\data.txt"))) {
                while (true) {
                    String line = reader.readLine();
                    if (line == null) {
                        break;
                    }
                    list.add(line);
                }
            }

            System.out.println("ArrayList size: " + list.size());

            for (String s : list) {
                System.out.println(s);
            }

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}

/*
                   
run:
      
ArrayList size: 3
java,javascript
c,c++,c#
php,html,css
          
 */

 



answered Dec 17, 2016 by avibootz

Related questions

1 answer 359 views
1 answer 424 views
1 answer 410 views
1 answer 207 views
2 answers 372 views
...