Solved

access to text file inside a jar

Posted on 2007-11-27
6
1,082 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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