Solved

Opening a text file located somewhere on the Internet

Posted on 2001-06-18
3
189 Views
Last Modified: 2010-03-31
Hello,

Is it possible to create a read-only java.io.File object from a URL?  e.g Let's say I wanted to read the contents of http://www.microsoft.com/index.html (it's a text file), Can I somehow create a java.io.File object from the URL?

Rob.
0
Comment
Question by:robertod031297
3 Comments
 
LVL 5

Accepted Solution

by:
Calron earned 200 total points
ID: 6202098
The following code is from the java tutorial:

http://enterprise/java/tutorial/networking/urls/readingURL.html

import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {
     URL yahoo = new URL("http://www.yahoo.com/");
     BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    yahoo.openStream()));

     String inputLine;

     while ((inputLine = in.readLine()) != null)
         System.out.println(inputLine);

     in.close();
    }
}

it demonstrates an example of how to read from a URL. It doesn't write the result into a file, but once you have the string data that is not hard to do.
0
 
LVL 3

Expert Comment

by:sghosh092199
ID: 6208855
You can write the contents of the URL as follows:

PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt")));

while ((inputLine = in.readLine()) != null)
{
        out.println(inputLine);

        out.flush();
}
out.close();
in.close();

Once you have created your file, you have to mark it as Read-only which can be done as follows:

File file = new File("myfile.txt");
file.setReadOnly();

Hope that helps!
   
 
0
 

Author Comment

by:robertod031297
ID: 6210123
Thanks to both of you.  The first solution worked perfectly so that's where the points should go.

Take Care,

Rob.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now