Solved

access to text file inside a jar

Posted on 2007-11-27
6
1,069 Views
Last Modified: 2013-12-15
my code reads from a text file
      String source_name =  "./src/"+name + ".txt";
               File source_file = new File(source_name);
        try {
          BufferedReader filereader = new BufferedReader(new FileReader(
            source_file.getAbsolutePath()));
}
it works fine in eclipse but when i create the jar file i does not work...
the text files are in my source directory of the project

thank you
0
Comment
Question by:maarmolak
6 Comments
 
LVL 11

Accepted Solution

by:
elfe69 earned 125 total points
ID: 20362060
Use getResourceAsStream():

String source_name =  name + ".txt";
BufferedReader filereader = new BufferedReader(InputStreamReader(System.class.getResourceAsStream(source_name)));

where System.class can be replaced by this.getClass() if you are not in a static method or any static class reference in a static method.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20362128
Either use the absolute path or load the stream using getClass().getResourceAsStream("xxxx");
0
 

Author Comment

by:maarmolak
ID: 20362132
thanx for the reply
but it wont compile...inputstreamReader(inputStream) is undefined


 BufferedReader filereader = new BufferedReader(InputStreamReader(this.getClass().getResourceAsStream(source_name)));
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Expert Comment

by:objects
ID: 20362166
make sure u have the requireded imports

import java.io.*;
0
 
LVL 11

Expert Comment

by:elfe69
ID: 20362185
I forgot to tell you that the root of the path is the root in the jar file and should be added:

String source_name =  "/" + name + ".txt";
BufferedReader filereader = new BufferedReader(InputStreamReader(System.class.getResourceAsStream(source_name)));
0
 

Author Comment

by:maarmolak
ID: 20365475
when i put in   " this.getClass" it wont work but with out the "this" works perfectly
thanks guys
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java  and programming certification ? 4 86
Unable to start eclipse ? 17 132
CKEditor - will not function 7 63
expectj telnet failing 5 26
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now