Solved

Opening a text file located somewhere on the Internet

Posted on 2001-06-18
3
191 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 40
Java - Why doesn't this JFrame work 3 40
javap bin 2 25
factorial example 4 34
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 …
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

930 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

14 Experts available now in Live!

Get 1:1 Help Now