i*****1 发帖数: 7 | 1 Da xia,
My applications has different packages, and I want a different property file
in each package. How can I read/write some files located at the same directory
of the class file?
I can use:
InputStream in = this.getResource("filename").openStream();
To get the file content sucessfully.
But how can I change the file content? I tried
FileOutputStream out = new FileOutputStream(this.getResource("filename").
toString());
this.getResource("filename").toString() return a String like file:C://... |
g**********y 发帖数: 14569 | 2 See getClass().getResource()
directory
【在 i*****1 的大作中提到】 : Da xia, : My applications has different packages, and I want a different property file : in each package. How can I read/write some files located at the same directory : of the class file? : I can use: : InputStream in = this.getResource("filename").openStream(); : To get the file content sucessfully. : But how can I change the file content? I tried : FileOutputStream out = new FileOutputStream(this.getResource("filename"). : toString());
|
c*****t 发帖数: 1879 | 3 1. You cannot change the content of a jar file package.
2. A normal way of getting resource from jar package is:
myClass.class.getResourceAsStream ("path");
if the resource file is located in the same directory as myClass,
then all you need to is to specify the name of the file as path.
directory
【在 i*****1 的大作中提到】 : Da xia, : My applications has different packages, and I want a different property file : in each package. How can I read/write some files located at the same directory : of the class file? : I can use: : InputStream in = this.getResource("filename").openStream(); : To get the file content sucessfully. : But how can I change the file content? I tried : FileOutputStream out = new FileOutputStream(this.getResource("filename"). : toString());
|
m******t 发帖数: 2416 | 4 I wouldn't try to do this if I were you. |
g**********y 发帖数: 14569 | 5 I think you can change the jar file package, if you use ZipFile* (forget the
exact class).
【在 c*****t 的大作中提到】 : 1. You cannot change the content of a jar file package. : 2. A normal way of getting resource from jar package is: : myClass.class.getResourceAsStream ("path"); : if the resource file is located in the same directory as myClass, : then all you need to is to specify the name of the file as path. : : directory
|
g**********y 发帖数: 14569 | 6 I find that I always learn things in hard way :-) If never stumbled, it is
difficult to understand why things are done in "right" way.
【在 m******t 的大作中提到】 : I wouldn't try to do this if I were you.
|
g*****g 发帖数: 34805 | 7 It's very inefficient to change content in a compressed file.
Every time you extract it, change it and recompress it.
jar file was supposed for read-only. If you do have an XML or
configuration which you'll change frequently, just leave it
out of package.
Every time you are obssessed to do things like this, ask yourself,
is it worth the trouble?
【在 g**********y 的大作中提到】 : I find that I always learn things in hard way :-) If never stumbled, it is : difficult to understand why things are done in "right" way.
|
m******t 发帖数: 2416 | 8
I hear you and I'm perfectly fine with people doing crazy things -
except in many cases like this when somebody did something crazy,
geuss what, I happen to be the one who would learn in the hard way that
some idiot did something crazy - and that's what pisses me off.
【在 g**********y 的大作中提到】 : I find that I always learn things in hard way :-) If never stumbled, it is : difficult to understand why things are done in "right" way.
|
m******t 发帖数: 2416 | 9
You guys... this isn't even about writing into a compressed file -
who says there is a compressed file to begin with? What if the class
is loaded from a URL? What if the class is loaded from a jar that
is loaded from a URL?
【在 g*****g 的大作中提到】 : It's very inefficient to change content in a compressed file. : Every time you extract it, change it and recompress it. : jar file was supposed for read-only. If you do have an XML or : configuration which you'll change frequently, just leave it : out of package. : Every time you are obssessed to do things like this, ask yourself, : is it worth the trouble?
|