Solved

access to text file inside a jar

Posted on 2007-11-27
6
1,062 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
Comment Utility
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
Comment Utility
Either use the absolute path or load the stream using getClass().getResourceAsStream("xxxx");
0
 

Author Comment

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


 BufferedReader filereader = new BufferedReader(InputStreamReader(this.getClass().getResourceAsStream(source_name)));
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 92

Expert Comment

by:objects
Comment Utility
make sure u have the requireded imports

import java.io.*;
0
 
LVL 11

Expert Comment

by:elfe69
Comment Utility
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
Comment Utility
when i put in   " this.getClass" it wont work but with out the "this" works perfectly
thanks guys
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

772 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

14 Experts available now in Live!

Get 1:1 Help Now