URI is not hierarchical

A

Abs

Hi!

I have a problem with the following line of code. Java gives me a "URI
is not hierarchical" error.

Code:
File sourceFile = new File(new
URI(this.getClass().getClassLoader().getResource("images/previous.gif").toString()));

Is there any other way to create this file ?


Thanks
 
P

pfalstad

What are you trying to do? The resource isn't a file, it's just a
stream of data in the jar file. You can create the File object by just
taking the return value from getResource() and calling the File
constructor with that URL object. But you can't do anything useful
with the File object because it's not a real file. If you want to read
the data from the resource, use getResourceAsStream() instead of
getResource() and then read the data from the resulting InputStream.
 
A

Abs

En/na (e-mail address removed) ha escrit:
What are you trying to do? The resource isn't a file, it's just a
stream of data in the jar file. You can create the File object by just
taking the return value from getResource() and calling the File
constructor with that URL object. But you can't do anything useful
with the File object because it's not a real file. If you want to read
the data from the resource, use getResourceAsStream() instead of
getResource() and then read the data from the resulting InputStream.

try {
File sourceFile = new File(new
URI(this.getClass().getClassLoader().getResource("images/previous.gif").toString()));
File targetFile = new File(file,sourceFile.getName());
copyFile(sourceFile,targetFile);
} catch (URISyntaxException e) {
e.printStackTrace();
}

I want to copy some image files from my app jar to a location of choice,
to build a HTML gallery.
 
J

John C. Bollinger

Abs said:
En/na (e-mail address removed) ha escrit:


try {
File sourceFile = new File(new
URI(this.getClass().getClassLoader().getResource("images/previous.gif").toString()));

File targetFile = new File(file,sourceFile.getName());
copyFile(sourceFile,targetFile);
} catch (URISyntaxException e) {
e.printStackTrace();
}

I want to copy some image files from my app jar to a location of choice,
to build a HTML gallery.

Did you read the rest of pfalstad's response? Any File you might manage
to create in this way is unlikely to be useful to you anyway. To copy a
file from inside the jar to outside it you can, as pfalstad wrote, use
getResourceAsStream() to get an InputStream for reading the file
contents. Then create an output stream for writing the new file (which
you might do with use of a File object) and copy all the bytes from
input to output.


John Bollinger
(e-mail address removed)
 
A

Abs

John said:
Did you read the rest of pfalstad's response? Any File you might manage
to create in this way is unlikely to be useful to you anyway. To copy a
file from inside the jar to outside it you can, as pfalstad wrote, use
getResourceAsStream() to get an InputStream for reading the file
contents. Then create an output stream for writing the new file (which
you might do with use of a File object) and copy all the bytes from
input to output.

Thanks
 
T

Tor Iver Wilhelmsen

Abs said:
I want to copy some image files from my app jar to a location of choice,
to build a HTML gallery.

Then use getResourceAsStream() to get at the content, then a
FileOutputStream to write it.
 

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

Forum statistics

Threads
474,260
Messages
2,571,038
Members
48,768
Latest member
first4landlord

Latest Threads

Top