Solved

Reading a text file from JAR file

Posted on 2001-06-10
5
187 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 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
Java Server Faces parameter pass? 6 48
varialbe initialization 11 32
throw exception 21 42
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

816 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now