?
Solved

access to text file inside a jar

Posted on 2007-11-27
6
Medium Priority
?
1,086 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 500 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
Technology Partners: 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!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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