0

Consider following

Code

    private String url = "https://celestrak.org/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.org/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.

Community
  • 1
  • 1
Junaid
  • 2,572
  • 6
  • 41
  • 77

2 Answers2

1

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();
    }
}
Abhishek
  • 2,485
  • 2
  • 18
  • 25
  • Yeah. Got it. (y) – Junaid Nov 25 '16 at 05:04
  • @Junaid does this solve your issue? i have updated my answer to include a different approach also – Abhishek Nov 25 '16 at 05:11
  • How does the first solution work? Does it have embedded `\n` in the `data` String? – Scary Wombat Nov 25 '16 at 05:13
  • Well I used slightly different approach http://stackoverflow.com/questions/6259339/how-to-read-a-text-file-directly-from-internet-using-java . – Junaid Nov 25 '16 at 05:19
  • @ScaryWombat please check my updated answer to the first solution... all he had to do was to split the response at ''\n" – Abhishek Nov 25 '16 at 05:31
  • @Junaid yes there will be many number of solutions to a given question... :P – Abhishek Nov 25 '16 at 05:34
  • @ScaryWombat no it doesn't need... i have run the main method in my system before answering. – Abhishek Nov 25 '16 at 05:35
  • @ScaryWombat adding the double back slash didn't make any difference in the output... can you please explain if it would make any difference in a different scenario ?? – Abhishek Nov 25 '16 at 05:40
  • @ScaryWombat according to --Alan Moore comment adding a double back slash isn't necessary when used as a string literal http://stackoverflow.com/a/455750/4626402 also in this answer also http://stackoverflow.com/a/4539914/4626402 --marcog says the same. – Abhishek Nov 25 '16 at 05:54
0

Since the file is already delimited by line separator, we can simple take the input stream from URL to read the contents

    String url = "https://celestrak.com/NORAD/elements/resource.txt";
    List<String> text = new BufferedReader(new InputStreamReader(new URL(url).openStream())).lines().collect(Collectors.toList());

To convert to a String

    String content = new BufferedReader(new InputStreamReader(new URL(url).openStream())).lines()
            .collect(Collectors.joining(System.getProperty("line.separator")));
Saravana
  • 12,647
  • 2
  • 39
  • 57