Solved

File object from URL object

Posted on 2004-09-06
23
225 Views
Last Modified: 2010-08-05
Hi,
   What's the easiest way to get a File object from a URL object?


Thanks!
0
Comment
Question by:petepalmer
  • 8
  • 6
  • 4
  • +2
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989228
String file = url.getFile();
File f = new File(file);
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11989254
I tried that and it dropped dead....


hmmm, I'm going to go recheck what I put - maybe I missed something....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989271
Make sure the path returned resolves in the file system
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11989301
Does the URL refer to a remote location?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11989302
new File(url.getFile());
0
 
LVL 37

Expert Comment

by:zzynx
ID: 11989304
Ooops, the refreshing problem again... :°(
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11989307
Okay my bad, it wasn't the file that was the problem.... it's just the way I was trying to parse it :


URL fileURL = new URL(args[0]);

            File file = new File(fileURL.getFile());


            parser.parse(file.toString());


I've tried numerous things but this is what I have ended up with so far lol
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11989312
The URL does point to a remote website
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11989315
if the file is placed on the server then you have to download it
try this
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20736476.html
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11989330
Extract from the above mentioned Link

java.net.URL url = new java.net.URL("http://www.yahoo.com/");
java.net.URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                connection.getInputStream()));
String inputLine;
StringBuffer html = new StringBuffer("");
while ((inputLine = in.readLine()) != null)
        html.append(inputLine);

out.print("read=" + html.toString());
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11989332
Surely there has to be an easier way than that? I only want to pass the xml document to the parser....
0
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.

 
LVL 30

Expert Comment

by:Mayank S
ID: 11989335
>> The URL does point to a remote website

Could cause problems if there is a proxy or a firewall in between.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11989338
Might be better to download it and save a temporary copy.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 11989339
You can use the createTempFile () method of the File class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989340
>>The URL does point to a remote website

Then you may want to do

    String[] atoms = file.toString().split("/");
    String name = atoms[atoms.length - 1];
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 11989345
>>Surely there has to be an easier way than that? I only want to pass the xml document to the parser....

Use the url ctor of the parser
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11989351
Well firewall isn't a problem - using good old http for moving files around - ftp is too "messy" :)

How would I use the createTempFile() method  ?

*goes to check the docs in the meantime*
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989352
>>Then you may want to do

Ignore that  - i didn't think you were trying to access a remote file as a local one
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989358
You don't need to create files at all - just read the URL directly by passing it to the ctor of the parser
0
 
LVL 18

Expert Comment

by:armoghan
ID: 11989422
In my above mentioned example, you can get the HTML as a String and pass it on to the parser
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989451
No need for that. Nearly all parsers will accept a URL to the ctor - in fact they'd prefer a URL to a File
0
 
LVL 1

Author Comment

by:petepalmer
ID: 11989572
Okay, I just put the URL in there - and of course it worked perfectly. I now feel a complete idiot for not trying that in the first place lol - I have no idea why I decided to try and be clever by doing all that stuff with it.....

ah well, that isn't a mistake I'll be repeating in the near future :)

Thanks all!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11989635
:-)
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
wordmultiple challenge 12 116
what is a "java.lang.System Property"   ? 20 56
jar file executable 12 37
why cannot we forward request once the response is committed 2 33
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…
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 …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

943 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

11 Experts available now in Live!

Get 1:1 Help Now