Reading/Wrting on a file in classpath

C

Chanchal

Hi All,

How can I write to a file in class path? I do not have a physical
path.

I know that a file in class path can be read as
InputStream resourceAsStream = getClass().getResourceAsStream("/
mypackage/mysubpackage/myfile.txt");

Is there any such way to write to a file in class path and which is
inside a package?

Thanks in advance

Chanchal
 
J

John B. Matthews

Chanchal said:
How can I write to a file in class path? I do not have a physical
path.

I know that a file in class path can be read as InputStream
resourceAsStream = getClass().getResourceAsStream("/
mypackage/mysubpackage/myfile.txt");

Is there any such way to write to a file in class path and which is
inside a package?

You might look at this thread:

<http://groups.google.com/group/comp.lang.java.help/browse_frm/thread/315
5a6ce4841c626>
 
A

Arne Vajhøj

Chanchal said:
How can I write to a file in class path? I do not have a physical
path.

I know that a file in class path can be read as
InputStream resourceAsStream = getClass().getResourceAsStream("/
mypackage/mysubpackage/myfile.txt");

Is there any such way to write to a file in class path and which is
inside a package?

No.

Most implementations will not allow you to update a jar file
open by the classloader.

Arne
 
M

Mark Rafn

Chanchal said:
How can I write to a file in class path? I do not have a physical
path.

You can get a physical path with ClassLoader.findResource(), but there's no
guarantee that it exists or is useful - it could be inside a jar, loaded over
the web, a temporary file that no longer exists on disk, or even a class
that's generated on the fly without a physical bytecode sequence on disk.
I know that a file in class path can be read as
InputStream resourceAsStream = getClass().getResourceAsStream("/
mypackage/mysubpackage/myfile.txt");

use getClass().getClassLoader().findResource("path") instead.
Is there any such way to write to a file in class path and which is
inside a package?

This depends entirely on what the file is, and what you expect from writing
it. Generally, a class won't get reloaded even if it's changed on disk. In
many cases, though, you can throw away the whole classloader and make a new
one to load the new version. Some classloaders on some OSs lock classfiles,
so you can't overwrite or edit them. In this case, you'd need to write a
custom ClassLoader to allow it, or throw away the classloader before writing
(and hope it got GC'd soon enough to release OS locks).
 
R

Roedy Green

Is there any such way to write to a file in class path and which is
inside a package?
the classpath is a system property.

See http://mindprod.com/jgloss/properties.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
PM Steven Harper is fixated on the costs of implementing Kyoto, estimated as high as 1% of GDP.
However, he refuses to consider the costs of not implementing Kyoto which the
famous economist Nicholas Stern estimated at 5 to 20% of GDP
 
C

Chanchal

No.

Most implementations will not allow you to update a jar file
open by the classloader.

Arne

I didn't mean that the file to be written is inside a jar file. Just
that i do not know the physical location of the file.

Chanchal
 
Joined
Dec 29, 2010
Messages
1
Reaction score
0
Here is some sample code which can be used to open and write to a file which is located on the classpath (but not in a jar file) therefore avoiding the need to hard-code the actual file path. Note that the file being written is actually in a directory which is located on the classpath; in the sample below the file pre-exists.
String filenameOnClasspath = "/myfile.txt" // Located in a directory which is on the classpath
URL url = getClass().getResource(filename);
String fullyQualifiedFilename = url.getFile();
PrintWriter pw = new PrintWriter(new FileWriter(new File(fullyQualifiedFilename)));
pw.println("some text");

The code above cannot be in a static method or static block since it calls the Object.getClass() method.
 
Last edited:

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top