How do I retrieve a URL in Java?

URL Stream Text in java?

  • Hello am trying to make a news feed am having problems making a loop that displays next line in a text file. i can only make it go though first line i tryed alot of things i tryed making a array list and display like that. nothing worked. anyone can help me try { URL url = new URL("http://test.txt");//news url BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); String arraylist = br.readLine(); if(arraylist != null) this.news = arraylist; } catch(Exception e){e.printStackTrace();} } I tryed diffent ways like this: this one displays nothing. not even first line like the first URL url = new URL ("news.txt"); URLConnection uc = url.openConnection( ) ; BufferedReader br = new BufferedReader(new InputStreamReader ( uc.getInputStream( ) ) ); while(true) { String line = br.readLine( ) ; if (line==null) break; System.out.println(line); }

  • Answer:

    Try this first just to make sure you're seeing any data from your stream. This reads in characters without worrying about the newline. Note that if you have binary (non-text) data coming from your stream, you console is going to make weird beeps. char[] data=new char[1024]; int cnt=-1; while((cnt=br.read(data)!=-1) { System.out.write(data,0,cnt); } BTW, the best idiom for reading from a BufferedReader is this: String line=null; while((line=br.readLine())!=null) { ... }

Andreas at Yahoo! Answers Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.