0

I need to read a file with this format:

[user1, user2, user3]

Then I need to store it in an ArrayList. I have no idea about reading files and so..

Thanks for the help and sorry for my bad english :)

TheL0w3R
  • 43
  • 9
  • http://stackoverflow.com/questions/2788080/reading-a-text-file-in-java – DHerls Feb 23 '15 at 15:56
  • Hi and welcome to SO! I suggest you to crawl the web a bit before asking. You can just google "java read file line by line", and so on. There's a lot of tutorials + the API docs to learn about how to use it. As it stands your question is a bit broad to answer (what difficulties do you have?, what didn't work? , etc.). – Alexis C. Feb 23 '15 at 15:58
  • I searched but couldn't make it working. The file has the ArrayList format. I just need to get all the content of the file and load it in an ArrayList. My idea is having a method like getUsers(String file) and when called, return an ArrayList with all the users inside. Is that possible? – TheL0w3R Feb 23 '15 at 16:01

1 Answers1

0

Okay guys, I wrote some code and I got this:

    public static String getGroups(int index) {
    try {
        for (int i = 0; i < countLines(); i++) {
            String[] content = new String[countLines()];
            File file = new File(getIndexValues(i, 1));
            try {
                FileReader reader = new FileReader(file);
                char[] chars = new char[(int) file.length()];
                reader.read(chars);
                content[i] = new String(chars);
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return content[index];
        }
    } catch (IOException ex) {
        Logger.getLogger(Utils.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

It returns all the content in the file I want. I have a list of files so if I type getGroups(0) it will take the first line of the index file and will look for it's path to load it.

Now, how can I put the output in an ArrayList? I need to load it in a JList.

TheL0w3R
  • 43
  • 9