简体   繁体   中英

JAVA Read file from webserver and multiple strings

So on our webserver we have an txt that has multiple lines. Each line has his own UUID (String) and we need to read that out in JAVA.

But every attempt results in just 1 line that is read so 1 string. How can we properly read multiple lines from a txt file of a webserver?

This is our code.

    static URL url;

static {
    try {
        url = new URL("https://example.com/test.txt");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
}

static String text;

static {
    try {
        text = new Scanner( url.openStream() ).useDelimiter(",").next();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Reading Directly from a URL

After you've successfully created a URL, you can call the URL's openStream() method to get a stream from which you can read the contents of the URL. The openStream() method returns a java.io.InputStream object, so reading from a URL is as easy as reading from an input stream.

import java.net.*;
import java.io.*;

public class URLReader {

public static void main(String[] args) throws Exception {

    URL oracle = new URL("http://www.url.com/file.txt");
    BufferedReader in = new BufferedReader(
    new InputStreamReader(oracle.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM