954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Read a text file from a website

I need to open a .txt file from a website. I tried this code:

int len = 0;
                FileReader fr = new FileReader("http://www.cinndev.com/testFile.txt"); 
                BufferedReader br = new BufferedReader(fr); 
                String str = "";
                while ((str = br.readLine()) != null) {
                    System.out.println(str);
                    len++;
                }


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

Thanks!

Ghost
Posting Whiz
352 posts since Aug 2004
Reputation Points: 12
Solved Threads: 2
 

try

try {
    // Create a URL for the desired page
    URL url = new URL("http://www.cinndev.com/testFile.txt");

    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
    in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You