Solved

Opening a text file located somewhere on the Internet

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 54
Java DateChooser? 3 36
jar file executable 12 52
web application structure 18 77
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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 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