Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Accessing Text Files in JAR

Posted on 2003-03-11
Medium Priority
Last Modified: 2010-03-31
My program uses the RandomAccessFile method to access data from text files I have. Currently, I have to convert all my classes and text files into a single JAR file. As my program used to point to a physical location on the hard-disk (e.g. "C:\data") where the files were stored, I must now modify my original program in order to make it now point to the text files in the JAR. How do I go about doing this?
Is it possible to Access these text files in the JAR without using InputStream, and still allow my RandomAccessFile method to run as normal?
Question by:79nic
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
LVL 92

Expert Comment

ID: 8109604
u cannot use RandomAccessFile to access a file in a jar.

Expert Comment

ID: 8109613

Why don't you want to use InputStream?

It's fairly easy to access text files that exist in the jar your app runs from, I'll show you how if you accept InputStream :)

- bjorn

Expert Comment

ID: 8109696
JarInputStream would fit just fine...
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.


Expert Comment

ID: 8112111
If it is in the same Jar as your application, this is the easier solution:

InputStream keyInStream;
keyInStream = getClass().getClassLoader().getResourceAsStream("security/mycustom.keystore")

Author Comment

ID: 8116015
ok, say i accept InputStream, how would I be able to then read the data from my text file that is delimited. Instead of using the RandomAccessFile method that I use below, wat can perform the same functions for my current situation.

      RandomAccessFile raf = new RandomAccessFile(str_FullFileName, "r"); 0 ); // Starts at the beginning of the file
      String thisString = new String();
      thisString = raf.readLine();
        thisString = raf.readLine();
        //split line up start
        fir_comma_pos = thisString.indexOf(delimiter);
        sec_comma_pos = thisString.indexOf(delimiter,fir_comma_pos+1);
        third_comma_pos = thisString.indexOf(delimiter,sec_comma_pos+1);
        fourth_comma_pos = thisString.indexOf(delimiter,third_comma_pos+1);
        fifth_comma_pos = thisString.indexOf(delimiter,fourth_comma_pos+1);

        1st_variable = Long.parseLong(thisString.substring(0,fir_comma_pos));
        2nd_variable = Integer.parseInt(thisString.substring(fir_comma_pos+1,sec_comma_pos));

LVL 92

Expert Comment

ID: 8116185
Just use a BufferedReader:

BufferedReader raf = new BufferedReader(new FileReader(str_FullFileName));
LVL 92

Accepted Solution

objects earned 75 total points
ID: 8116191
Sorry that should be:

BufferedReader raf = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(filename)));


Author Comment

ID: 8117734
thanks man.... cheers....

By the way, for BufferReader class, I can't use method
seek(), getFilePointer(), or length() that I used with my RandomAccessFile, any further modifications I should make....

Thanks once again....
LVL 92

Expert Comment

ID: 8117775
no they are specific to RAF.

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

705 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