943,824 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8327
  • Java RSS
Aug 29th, 2008
0

Read a text file from a website

Expand Post »
I need to open a .txt file from a website. I tried this code:
Java Syntax (Toggle Plain Text)
  1. int len = 0;
  2. FileReader fr = new FileReader("http://www.cinndev.com/testFile.txt");
  3. BufferedReader br = new BufferedReader(fr);
  4. String str = "";
  5. while ((str = br.readLine()) != null) {
  6. System.out.println(str);
  7. len++;
  8. }

but the program froze on me. What is the best way to do this?

Thanks!
Similar Threads
Reputation Points: 12
Solved Threads: 2
Posting Whiz
Ghost is offline Offline
352 posts
since Aug 2004
Aug 29th, 2008
0

Re: Read a text file from a website

try
Java Syntax (Toggle Plain Text)
  1. try {
  2. // Create a URL for the desired page
  3. URL url = new URL("http://www.cinndev.com/testFile.txt");
  4.  
  5. BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  6. String str;
  7. while ((str = in.readLine()) != null) {
  8. System.out.println(str);
  9. }
  10. in.close();
  11. } catch (MalformedURLException e) {
  12. } catch (IOException e) {
  13. }
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help with Servlets
Next Thread in Java Forum Timeline: confusion with char datatype





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC