Deepankar Bajpeyi
Deepankar Bajpeyi

Reputation: 5859

How to populate an ArrayList from words in a text file?

I have a text file containing words separated by newline , like the following format:

>hello  
>world  
>example 

How do i create an ArrayList and store each word as an element?

Upvotes: 1

Views: 14097

Answers (6)

Jon Skeet
Jon Skeet

Reputation: 1500495

The simplest way is to use Guava:

File file = new File("foo.txt");
List<String> words = Files.readLines(file, Charsets.UTF_8);

(It's not guaranteed to be an ArrayList, but I'd hope that wouldn't matter.)

Upvotes: 3

daniel owens
daniel owens

Reputation: 31

public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub

    File file = new File("names.txt");
    ArrayList<String> names = new ArrayList<String>();
    Scanner in = new Scanner(file);
    while (in.hasNextLine()){
        names.add(in.nextLine());
    }
    Collections.sort(names);
    for(int i=0; i<names.size(); ++i){
        System.out.println(names.get(i));
    }

Upvotes: 3

Adam
Adam

Reputation: 36703

I'm sure they're lots of libraries that do this with 1 line, but here's a "pure" Java implementation:

Notice that we've "wrapped"/"decorated" etc. a standard FileReader (which only has read one byte at a time) with a BufferedReader which gives us a nicer readLine() method.

BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(
            new FileInputStream("test.txt"),
            Charset.forName("ISO-8859-1")));
    List<String> lines = new ArrayList<String>();
    String line;
    while ((line = reader.readLine()) != null) {
        lines.add(line);
    }
    System.out.println(lines);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (reader != null) {
        reader.close();
    }
}

Upvotes: 0

Tom
Tom

Reputation: 4180

I put the file at "C:\file.txt"; if you run the following it fils an ArrayList with the words and prints them.

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


public class Main {

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\file.txt");
        BufferedReader br = new BufferedReader(new FileReader(file));
        List<String> lines = new ArrayList<String>();
        String line = br.readLine();
        while(line != null) {
            lines.add(line.replace(">", ""));
            line = br.readLine();
        }
        for(String l : lines) {
            System.out.println(l);
        }
    }

}

Upvotes: 0

amit
amit

Reputation: 178441

You can use apache commons FileUtils.readLines().

I think the List it returns is already an ArrayList, but you can use the constructor ArrayList(Collection) to make sure you get one.

Upvotes: 3

dexametason
dexametason

Reputation: 1133

You read the file line-by-line, create an ArrayList for Strings, and add line.substring(1) to the defined ArrayList if line.length>0.

Upvotes: 0

Related Questions