Solved

Reading a text file from JAR file

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 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

20 Experts available now in Live!

Get 1:1 Help Now