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!

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) {
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.