Redian新闻
>
How to write a file to the same directory of the class file?
avatar
How to write a file to the same directory of the class file?# Java - 爪哇娇娃
i*1
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://...
avatar
g*y
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());

avatar
c*t
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());

avatar
m*t
4
I wouldn't try to do this if I were you.
avatar
g*y
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

avatar
g*y
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.
avatar
g*g
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.

avatar
m*t
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.

avatar
m*t
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?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。