Solved

access to text file inside a jar

Posted on 2007-11-27
6
1,073 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet doXXX methods 3 49
split string containing \r\n in Java 46 44
Apps blocked by Java 9 81
maven module vs maven project 3 23
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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