Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Opening a text file located somewhere on the Internet

Posted on 2001-06-18
3
Medium Priority
?
203 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
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.
Suggested Courses

604 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