Solved

Opening a text file located somewhere on the Internet

Posted on 2001-06-18
3
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

726 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