?
Solved

Reading a text file from JAR file

Posted on 2001-06-10
5
Medium Priority
?
194 Views
Last Modified: 2013-11-23
I would like to put a text file into the JAR file. How do I read its content from the JAR, using FileInputStream?
0
Comment
Question by:yongsing
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
ID: 6175554
Use the following, ensuring your jar file is in your classpath:

InputStream in = ClassLoader.getSystemResourceAsStream(textFileName);
0
 
LVL 2

Expert Comment

by:venkat2000120699
ID: 6175595
Here is a sample solution for ur problem.

JarTest.html
-------------

<HTML>
<APPLET  CODE = "JarTest.class" archive = "JarTest.jar" WIDTH = 300 HEIGHT = 300 >
</APPLET>
</body>
</html>

JarTest.java
-------------

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class JarTest extends Applet {

    public void init() {

          try {
         
              InputStream in = JarTest.class.getResourceAsStream("JarTest.txt");
                BufferedReader br =new BufferedReader(new InputStreamReader(in));
                String line;
               
                while( (line = br.readLine() ) !=null)
                    System.out.println(line);
                    //dumps the contents of the data file to the System Console
         
          } catch(IOException e) {
               System.out.print(e + "This error has occurred \n");
          }
    }
}

JarTest.txt
-----------
This is line 1
This is line 2
This is line 3
This is last line.

This will print the contents of text file in Java console of the browser.

0
 
LVL 3

Expert Comment

by:shyamkumarreddy
ID: 6175718
Hi YongSing

Once u put those file in the jar. it is in compressed form. Hence u need to uncompress it first using temporary files. then read those files as usual.


Cheers
Shyam
0
 
LVL 8

Expert Comment

by:DennisBorg
ID: 6177739
:ping:
0
 
LVL 92

Expert Comment

by:objects
ID: 6184067
Thanks for the points :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 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