Junaid Junaid - 13 days ago 6
Java Question

Reading txt file online

Consider following

Code

private String url = "https://celestrak.com/NORAD/elements/resource.txt";

@Override
public Boolean crawl() {

try {

// Timeout is set to 20s
Connection connection = Jsoup.connect(url).userAgent(USER_AGENT).timeout(20 * 1000);
Document htmlDocument = connection.get();
// 200 is the HTTP OK status code
if (connection.response().statusCode() == 200) {
System.out.println("\n**Visiting** Received web page at " + url);
} else {
System.out.println("\n**Failure** Web page not recieved at " + url);
return Boolean.FALSE;
}
if (!connection.response().contentType().contains("text/plain")) {
System.out.println("**Failure** Retrieved something other than plain text");
return Boolean.FALSE;
}

System.out.println(htmlDocument.text()); // Here it print whole text file in one line

} catch (IOException ioe) {
// We were not successful in our HTTP request
System.err.println(ioe);
return Boolean.FALSE;
}

return Boolean.TRUE;
}


Output

SCD 1 1 22490U 93009B 16329.83043855 .00000228 00000-0 12801-4 0 9993 2 22490 24.9691 122.2579 0043025 337.9285 169.5838 14.44465946256021 TECHSAT 1B (GO-32) 1 25397U ....


I am trying to read an online-txt file (from https://celestrak.com/NORAD/elements/resource.txt). Problem is that while I print or save the body's text it prints whole online-txt file in one line. But I want to read it as splited by
\n
so that I can read it line by line. Am I making mistake while reading online-txt file?

I am using JSoup.

Answer

you can do it without using jsoup in the following manner:

public static void main(String[] args) {
    String data;
    try {
        data = IOUtils.toString(new URL("https://celestrak.com/NORAD/elements/resource.txt"));
        for (String line : data.split("\n")) {
            System.out.println(line);
        }
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

the above code uses org.apache.commons.io.IOUtils

if adding the commons library is a issue you can use the below code:

public static void main(String[] args) {
        URLReader reader;
        try {
            reader = new URLReader(new URL("https://celestrak.com/NORAD/elements/resource.txt"));
        BufferedReader bufferedReader = new BufferedReader(reader);
        String sCurrentLine;
        while ((sCurrentLine = bufferedReader.readLine()) != null) {
            System.out.println(sCurrentLine);
        }
        bufferedReader.close();
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}