-5

So I want to get a List like that:

Entry1
Entry2
Entry3
Entry4

All those entries are in a text file on my webserver.

So my plan is to read those out, and add every of them to the a ArrayList.

Edit: The thing is I don't know how to read more lines.

I have this code:

public static String getDataFromServer() throws IOException{
    String sourceLine = null;

    URL address = new URL("webserver");

    InputStreamReader pageInput = new InputStreamReader(address.openStream());
    BufferedReader source = new BufferedReader(pageInput);

    try {
        sourceLine = source.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sourceLine;
}

With this I can read out 1 Line. As you can see in my Example I would need to read out every line there is, since I want to add more lines later on without editing the Code everytime.

I hope its more clear now.

Thanks for any help.

iraphi
  • 13
  • 2
  • Are you planning on deploying the code on the webserver, or do you want to connect to the webserver and read the file? Perhaps you could edit the question to include this, to help users better answer. – Robert Bain Dec 06 '14 at 19:31
  • What is the question here? – Oliver Charlesworth Dec 06 '14 at 19:32
  • [How to read a text from a web page with Java?](http://stackoverflow.com/questions/9825798/how-to-read-a-text-from-a-web-page-with-java) and [Best way to read a text file](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file). There are hundreds, if not thousands, of how-to's out there already. – dimo414 Dec 06 '14 at 19:33
  • @RobertBain I edited the Question. I'm trying to load all entries into a ArrayList to use them in my programm. I don't want to add the entries directly into the code so I don't have to edit the code if I have a new entry. – iraphi Dec 06 '14 at 19:38
  • Cool @iraphi, I've provided the answer below. You just need to declare an ArrayList of strings - I've called it arrayList. – Robert Bain Dec 06 '14 at 19:40
  • @RobertBain Thank you that worked! – iraphi Dec 06 '14 at 19:44

1 Answers1

0

Here's what you're looking for:

while ((sourceLine = source.readLine()) != null) {
    arrayList.add(sourceLine);
}
Robert Bain
  • 7,287
  • 5
  • 30
  • 50