?
Solved

Reading a text file from JAR file

Posted on 2001-06-10
5
Medium Priority
?
191 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 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 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