Read a text file from a website

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 350
Reputation: Ghost is an unknown quantity at this point 
Solved Threads: 2
Ghost's Avatar
Ghost Ghost is offline Offline
Posting Whiz

Read a text file from a website

 
0
  #1
Aug 29th, 2008
I need to open a .txt file from a website. I tried this code:
  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!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,485
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Read a text file from a website

 
0
  #2
Aug 29th, 2008
try
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC