jar and fies

G

Guest

I create a jar file with all my classes and my image resources inside.
Package name: my_pack.

Now, I want to put inside jar file (and inside directory my_pack) a TXT file
which I open with this line:

BufferedReader f = new BufferedReader(new
FileReader("my_pack/a_read_only_txt_file.txt"));

but this line always throws an exception (file not found)

What I can do?
 
H

Hal Rosser

BufferedReader f = new BufferedReader(new
FileReader("my_pack/a_read_only_txt_file.txt"));

but this line always throws an exception (file not found)

What I can do?
I would play around with the file name (leave off the "my_pack/" part and
see if it workie den.
But i dunno nuttin'
 
P

Paul Lutus

Hal said:
I would play around with the file name (leave off the "my_pack/" part and
see if it workie den.
But i dunno nuttin'

With all respect, why did you post? The OP is trying to read a file from a
JAR archive.
 
P

Paul Lutus

- Chameleon - said:
I create a jar file with all my classes and my image resources inside.
Package name: my_pack.

Now, I want to put inside jar file (and inside directory my_pack) a TXT
file which I open with this line:

BufferedReader f = new BufferedReader(new
FileReader("my_pack/a_read_only_txt_file.txt"));

but this line always throws an exception (file not found)

What I can do?

Learn how to read files from JAR archives:

URL url = getClass().getResource(path/filename);
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader br = new BufferedReader(isr);
 
H

Hal Rosser

Paul Lutus said:
Hal Rosser wrote:

With all respect, why did you post? The OP is trying to read a file from a
JAR archive.

I was just trying to give him a starting point - ie: what I would try if I
was in his shoes.
That's exactly what I would have tried - if it did not work - I would try
something else-
But at least I tried - if I'm wrong - then post a positive reply - or - give
him another starting point - or something else to try.
 
H

Hal Rosser

URL url = getClass().getResource(path/filename);
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader br = new BufferedReader(isr);
If the text file is in a jar file - can you give an example of what "
(path/filename) " shown in your first line- would need to be ?
 
K

KC Wong

[Hal Rosser]
[Paul Lutus]
[Hal Rosser]
I was just trying to give him a starting point - ie: what I would try if I
was in his shoes.
That's exactly what I would have tried - if it did not work - I would try
something else-
But at least I tried - if I'm wrong - then post a positive reply - or - give
him another starting point - or something else to try.

That'll be confusing to the OP, and since this group is archived, it will
confuse others searching similar topics as well. It is important that Paul
pointed out your advice is incorrect.

You provided a solution that you haven't tried and have no idea if it'll
work (you said you dunno nuttin')... isn't that wasting the OP's time by
pointing him to a random direction? Instead of blindly guessing you can wait
for others to reply to this thread and learn from it.
 
K

KC Wong

BTW, I'm not saying one cannot post anything unless he's/her's 100%
correct... the solution need not be perfect (others may chime in and provide
a better solution, so you can learn from it too), but it should at least
work.
 
H

Hal Rosser

ok - point taken

KC Wong said:
[Hal Rosser]

[Paul Lutus] from

[Hal Rosser]
I was just trying to give him a starting point - ie: what I would try if I
was in his shoes.
That's exactly what I would have tried - if it did not work - I would try
something else-
But at least I tried - if I'm wrong - then post a positive reply - or - give
him another starting point - or something else to try.

That'll be confusing to the OP, and since this group is archived, it will
confuse others searching similar topics as well. It is important that Paul
pointed out your advice is incorrect.

You provided a solution that you haven't tried and have no idea if it'll
work (you said you dunno nuttin')... isn't that wasting the OP's time by
pointing him to a random direction? Instead of blindly guessing you can wait
for others to reply to this thread and learn from it.
 
K

KC Wong

URL url = getClass().getResource(path/filename);
If the text file is in a jar file - can you give an example of what "
(path/filename) " shown in your first line- would need to be ?

It is the path of the file inside the JAR file.

Type this command:
jar -tf <put path of Jar file here>

It will output a list of files inside the JAR. Locate your text file... that
line is what you should put in the getResource call.
 
H

Hal Rosser

Thanks - now I can't say 'I don't know nuttin'

KC Wong said:
It is the path of the file inside the JAR file.

Type this command:
jar -tf <put path of Jar file here>

It will output a list of files inside the JAR. Locate your text file... that
line is what you should put in the getResource call.
 
K

KC Wong

Thanks - now I can't say 'I don't know nuttin'

We programmers learn new things everyday :)
That's our way of survival in this ever changin computing world.
 
P

Paul Lutus

Hal said:
ok - point taken

I know you are probably annoyed at me right now, but I must add to what I
have already said ... PLEASE do not top-post. It makes it very difficult to
follow a thread's progress.
 
J

Jacob

I create a jar file with all my classes and my image resources inside.
Package name: my_pack.

Now, I want to put inside jar file (and inside directory my_pack) a TXT file
which I open with this line:

BufferedReader f = new BufferedReader(new
FileReader("my_pack/a_read_only_txt_file.txt"));

This is the correct approach:

InputStream stream = getClass().getResourceAsStream("/some/path/file.txt");
BufferedReader reader = new BufferedReader (new InputStreamReader (stream));

The path refers to files _within_ the jar. The files inside
the jar can be listed with "jar -tf myjar.jar" as already
suggested, BUT: don't forget the initial "/" when calling
getResourceAsStream().
 
G

Guest

I create a jar file with all my classes and my image resources inside.
Learn how to read files from JAR archives:

URL url = getClass().getResource(path/filename);
InputStreamReader isr = new InputStreamReader(url.openStream());
BufferedReader br = new BufferedReader(isr);


Thanks!
 
A

Andrew Thompson

BTW, I'm not saying one cannot post anything unless he's/her's 100%
correct... the solution need not be perfect (others may chime in and provide
a better solution, so you can learn from it too), but it should at least
work.

There is not always the ..
a) complete example
b) time
c) context
...needed to ensure that something 'at least works'.

There is always a value judgement to be
made when you are not sure of an answer,
post, don't post, post now, wait a while and
see if other answers turn up (perhaps losing
the thread entirely if they don't)?

Given a 'disclaimer', words to the effect of
'not sure but..', is I feel, the best solution.

The OP, and later readers, can take that as a
warning. The OP can try the suggestion or not,
the later readers can perhaps skip straight to
the other responses.

Please note that I myself have experienced both
sides of this equation, both being told I should
not have posted, while on others I have advised
folks that they perhaps should not be confusing
the OP further.

I just want to point out that there are no
'magic rules' that can be applied that will give
the best result in all cases.

"There is always an easy solution to every
human problem ¡X neat, plausible, and wrong."
H. L. MENCKEN
 

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