Solved

Reading a text file from JAR file

Posted on 2001-06-10
5
189 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
[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
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 50 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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 240
servlet example 17 59
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 77
restrict decimal places for double datatype 10 32
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
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…
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:

733 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