由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - How to write a file to the same directory of the class file?
相关主题
InputStream.read() 被block的问题(陷入无限等待)Re: how to upload a local file to web server?
How to append something on a file?how to open pipe files in java
[转载] create gif/png image in C or java编程新人求助—死循环了
急问如何append double 数据到txt文件问一个blocking IO的程序
Java练习题 7servlet读取directory的基本问题
java applet读网络文件的问题愣是笨到家----整不明白applet的path
File generated by Java cannot be read by Android AppRe: access host file in applet
Run Servlet with TomcatRe: How can I call another program from Java?
相关话题的讨论汇总
话题: file话题: directory话题: class话题: filename话题: write
进入Java版参与讨论
1 (共1页)
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?

1 (共1页)
进入Java版参与讨论
相关主题
Re: How can I call another program from Java?Java练习题 7
怎么从键盘输入整数或float?java applet读网络文件的问题
请教一个问题,thanks!File generated by Java cannot be read by Android App
java virtual machine 急问!!在线等Run Servlet with Tomcat
InputStream.read() 被block的问题(陷入无限等待)Re: how to upload a local file to web server?
How to append something on a file?how to open pipe files in java
[转载] create gif/png image in C or java编程新人求助—死循环了
急问如何append double 数据到txt文件问一个blocking IO的程序
相关话题的讨论汇总
话题: file话题: directory话题: class话题: filename话题: write