-1

I am supposed to read a few lines from a textfile and then store them in an arraylist. What is the best way to do such a thing? I must later be able to check each individual char.

Fjodor
  • 529
  • 1
  • 7
  • 19
  • 1. read the file line by line, 2. put each line into an array list, 3. enjoy - did you try that already? – Thomas Aug 21 '14 at 12:15
  • The best approach is writing code. – Maroun Aug 21 '14 at 12:15
  • Come on... do a little research. http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java – ortis Aug 21 '14 at 12:17
  • You cannot expect people here to do your homework. – Zyga Aug 21 '14 at 12:17
  • You can try to use a `Scanner` to read the file line by line, and store them in an `ArrayList` as you read them. Is you use java 7 or 8, you can use `Files.readAllLines()`. – Florent Bayle Aug 21 '14 at 12:17
  • possible duplicate of [How to read a large text file line by line using Java?](http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java) – fabian Aug 21 '14 at 12:25
  • I guess I should've mentioned that I have already written the scanner thing, I am just unfamiliar with arraylist and didn't find what I was looking for when I googled it. – Fjodor Aug 21 '14 at 12:33

4 Answers4

0

Below is the simple snippet to do your work :

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class ParseFile
    {            
        public static void main (String[] args)
        {
            Scanner scanner = null;
            ArrayList<String> store = new ArrayList<String>();
            try {
               scanner= new Scanner(new File("input.txt"));
                while (scanner.hasNext()) {
                    store.add(scanner.next());             
                }
            } catch (Exception e) {
                e.printStackTrace();            
            }
            finally{
                scanner.close();
            }

            for (String item : store) {
                System.out.println(item);
            }

        }
    }
Snehal Masne
  • 3,403
  • 3
  • 31
  • 51
  • Come on. This _looks like_ Java, but does not even compile! Also, resource management is bad, i.e. you need to close the Scanner in a finally block. Finally, what do you expect to see in that System.out line at the end? The lines added to the ArrayList?! No way ... – mgaert Aug 21 '14 at 12:39
  • I said its snippet which could do it. I didnt intend to spoon feed. I just wanted to give the direction. – Snehal Masne Aug 21 '14 at 12:42
  • Sure, NP. Sorry for sounding harsh. – mgaert Aug 21 '14 at 13:02
0

Use Apache commons io List lines = FileUtils.readLines(file, "UTF-8");

mgaert
  • 2,338
  • 21
  • 27
0

You can use Files.readAllLines(Path path)

Path path = //...
ArrayList<String> lines = new ArrayList<>(Files.readAllLines(path, Charset.defaultCharset()));

But using this approach, you'll have 2 lists in memory twice for a short time (i.e. during the execution of the ArrayList constructor).

If you don't need a modifiable list and don't need the list's type to be a ArrayList

List<String> lines = Files.readAllLines(path, Charset.defaultCharset());

works too, of course.

In java 8 you can of course use Streams too:

final ArrayList<String> lines = new ArrayList<>();
try (Stream<String> lineStream = Files.lines(path)) {
    lineStream.forEach(lines::add);
}

This way the lines are added to the list directly after they are read.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Good pointer! While Files is Java 7, readAllLines(Path) is only in Java 8, though ... need to use readAllLines(Path,CharSet) for Java 7... – mgaert Aug 21 '14 at 13:05
0

Try this:

 FileReader in = null;
    try
    {
        in = new FileReader("C:/Test.txt");
        BufferedReader br = new BufferedReader(in);
        ArrayList<String> lines = new ArrayList<String>();


        String s = br.readLine();
        while (s != null)
        {
            lines.add(s);
            s = br.readLine();

        }
        in.close();
    }
    catch (Exception ex)
    {
        Logger.getLogger(Read.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Kiki
  • 2,243
  • 5
  • 30
  • 43