Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Accessing Text Files in JAR

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?
0
79nic
Asked:
79nic
1 Solution
 
objectsCommented:
u cannot use RandomAccessFile to access a file in a jar.
0
 
borbjoCommented:

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
0
 
karlikaCommented:
JarInputStream would fit just fine...
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

InputStream keyInStream;
keyInStream = getClass().getClassLoader().getResourceAsStream("security/mycustom.keystore")
0
 
79nicAuthor Commented:
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.


try
    {
      RandomAccessFile raf = new RandomAccessFile(str_FullFileName, "r");
      raf.seek( 0 ); // Starts at the beginning of the file
      String thisString = new String();
      thisString = raf.readLine();
      do
      {
        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));

......
0
 
objectsCommented:
Just use a BufferedReader:

BufferedReader raf = new BufferedReader(new FileReader(str_FullFileName));
0
 
objectsCommented:
Sorry that should be:

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

0
 
79nicAuthor Commented:
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....
0
 
objectsCommented:
no they are specific to RAF.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now