0

How can I read a file into a array of String[] and then convert it into ArrayList?

I can't use an ArrayList right away because my type of list is not applicable for the arguments (String).

So my prof told me to put it into an array of String, then convert it.

I am stumped and cannot figure it out for the life of me as I am still very new to java.

  • Seems like a duplicate of http://stackoverflow.com/questions/19844649/java-read-file-and-store-text-in-an-array. In general, for converting an Array into a List, you can use `Arrays.asList(myArray);` – Mick Mnemonic Apr 05 '15 at 23:53
  • I keep getting the error "cannot convert List to List... The ArrayList holds Person objects FYI – Clarkie_12 Apr 06 '15 at 00:12
  • It would help if you shared the relevant code. But in short, you cannot read into a list of `Person`s from the file without populating each `Person` explicitly. It might be easiest to read the "raw" string lines in first and do the conversion of line -> `Person` afterwards. – Mick Mnemonic Apr 06 '15 at 00:15

2 Answers2

0
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by tsenyurt on 06/04/15.
 */
public class ReadFile
{
    public static void main(String[] args) {

        List<String> strings = new ArrayList<>();
        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("/Users/tsenyurt/Development/Projects/java/test/pom.xml"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                strings.add(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

there is a code that reads a file and create a ArrayList from it

http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

turshu
  • 44
  • 4
0

Well there are many ways to do that, you can use this code if you want have a List of each word exist in your file

public static void main(String[] args) {
    BufferedReader br = null;
    StringBuffer sb = new StringBuffer();
    List<String> list = new ArrayList<>();
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(
                "Your file path"));

        while ((sCurrentLine = br.readLine()) != null) {
            sb.append(sCurrentLine);
        }
        String[] words = sb.toString().split("\\s");
        list = Arrays.asList(words);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    for (String string : list) {
        System.out.println(string);

    }
}
victorkt
  • 13,992
  • 9
  • 52
  • 51