String to URL conversion ?

F

Frederick Smith

Hi there ... I am a bit new to this, and I have probably misunderstood the
use of the "toUrl" function - or I should not be using it at all

I have a program which reads a line of text from a file, and I would then
like to convert the resulting string to a URL, so I can read from that
location, and then get the next location from the file.

If I hard code the url in to the InputStreamReader the thing works ok and
reads from a single URL, but if I try to use the string which I read from
the file as a URL, I get all sort of error messages.

Any suggestions ?

frederick
 
E

Eric Capps

Frederick said:
Hi there ... I am a bit new to this, and I have probably misunderstood the
use of the "toUrl" function - or I should not be using it at all

I have a program which reads a line of text from a file, and I would then
like to convert the resulting string to a URL, so I can read from that
location, and then get the next location from the file.

If I hard code the url in to the InputStreamReader the thing works ok and
reads from a single URL, but if I try to use the string which I read from
the file as a URL, I get all sort of error messages.

Any suggestions ?

frederick

URL has a constructor that takes String as an argument, so if you have
file blah.dat, you can do:

BufferedReader b = new BufferedReader(new FileReader("blah.dat"));
URL u = new URL(b.readLine());
 
O

Oliver Wong

Frederick Smith said:
Hi there ... I am a bit new to this, and I have probably misunderstood the
use of the "toUrl" function - or I should not be using it at all

I have a program which reads a line of text from a file, and I would then
like to convert the resulting string to a URL, so I can read from that
location, and then get the next location from the file.

If I hard code the url in to the InputStreamReader the thing works ok and
reads from a single URL, but if I try to use the string which I read from
the file as a URL, I get all sort of error messages.

Any suggestions ?

Post the exact error message. See
http://riters.com/JINX/index.cgi/Su...estions_20on_20Newsgroups#RepeatErrorsExactly

- Oliver
 
S

Steve W. Jackson

Frederick Smith said:
Hi there ... I am a bit new to this, and I have probably misunderstood the
use of the "toUrl" function - or I should not be using it at all

I have a program which reads a line of text from a file, and I would then
like to convert the resulting string to a URL, so I can read from that
location, and then get the next location from the file.

If I hard code the url in to the InputStreamReader the thing works ok and
reads from a single URL, but if I try to use the string which I read from
the file as a URL, I get all sort of error messages.

Any suggestions ?

frederick

Well, there's a toURL method on the java.io.File class and one on
java.net.URI, so it's unclear what exactly you're doing there. And you
don't offer any details about these error messages.

As an aside: If your file has lines of URLs, I'm wondering why not use
something as simple as a LineNumberReader to read its lines...

Assuming the lines of your file are valid when used as a URL, you should
be able to read each line into a String and then pass that to the
constructor of either a URL or URI. I've found that the URI class is
often better for some purposes. URI contains a toURL method, and URL
contains a toURI method, making conversion between them simple.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top