954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to read from a text file,jar ing problem

hello all

how to read from a text file
which is not only run at the developer machine
but also run at the other machine
(after jar ing)
I have tried ,the code run at my own machine
but cannot find the text file on other machine

thank you

denny

dennysimon
Junior Poster
114 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Sorry to ask but what are you actually trying to achieve? Reading text file from JAR is simple, but you will just do it on the same machine where JAR is used as the file is mostly just some part of configuration or commentary since you cannot update it without extracting JAR and packing it back again...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

you need to create file through your code.

rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
 

hello all
I feel may be i better give further detail

I create a code which have combobox in it,
to make user can easily add items to the combobox,
I create a text file which
is read by the code at load ,splited the text content ,and then filled to the combobox's item
The code run at my machine ,I make it (the code) as a jar file.
but when the jar file copied to other machine ,the code cannot find the text file
So the combobox remain empty.
I have included the text file in the jar file
may be the path of the text file changing ? because the
directory structure in my machine is different from the
other machine ?


thank you

denny

dennysimon
Junior Poster
114 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

well, that is because you haven't created the text file there. is there a possibility that the elements will be different on another machine, or during another run of the application, or will it always have the same values?

also good to know, is whether every instance of the application will need to get all the same elements in those comboboxes or not.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

hello all
I just test to run in my own machine ,but from desktop
It's combobox remain empty .
so the code only run at the original directory,it was created

thank you

denny

dennysimon
Junior Poster
114 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

that's because that is the only place you've created that txt file with values. it's possible to, if you want every instance (even over several computers) to get the same values, to either hardcode them, or have each instance create it's own .txt file or to store the information in a centralized DB.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

hello all

the text file's content is only a line of city's names separated by comma

thank you

denny

dennysimon
Junior Poster
114 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

well yes, but will it contain exactly the same content, or can this depend on by who or where the application is used?

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

You would normally use the classloader's getResourceAsStream method - which has the advantage of immediately giving you an open InputStream on the file in the current jar. You'll find the doc in the usual places, but briefly:
in the code of a class that was loaded from the same jar...

InputStream in = this.getClass().getResourceAsStream("/myTextFile.txt");

If the file is in a folder within the jar then that would be "/myDirectory/myTextFile.txt"
When developing your code this also works outside a jar - as long as the file is in the same place relative to where the .class file was loaded from.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

hello all
the text file's content is the same until user add item into it,and the code
used in a stand alone machine

thank
denny

dennysimon
Junior Poster
114 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

Adding to a file in a jar is a more complex problem. Would it make sense to hold an initial/default list in the jar, but put each user's additions into a separate file in the user's home dir?

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

it might also be easier to have the list stored in a class instead of a text file. Enum's spring to mind :)

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

hello all the text file's content is the same until user add item into it,and the code used in a stand alone machine

thank denny


Well I guess you could store the text file within your packaged jar file, either by adding it during compile time by looking here: http://javaworkshop.sourceforge.net/chapter5.html#Creating+and+Viewing+a+Jar+File or by renaming your jar to *.zip extracting it placing text file within jar file and then rezipping the file and renaming to *.jar. You could then access the text file like so:

import java.io.*;
import java.util.*;

public class JavaApplication44 {

    public static void main(String args[]) throws IOException {
        List<String> list = JavaApplication44.readTextFromJar("d.txt");//text file is in smae folder as main class
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    public static List<String> readTextFromJar(String s) {
        InputStream is = null;
        BufferedReader br = null;
        String line;
        ArrayList<String> list = new ArrayList<String>();

        try {
            is = JavaApplication44.class.getResourceAsStream(s);
            br = new BufferedReader(new InputStreamReader(is));
            while (null != (line = br.readLine())) {
                list.add(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return list;
    }
}


once the user adds more data to the file you can just write it to an external location, or alternatively you could write a method that would extract and update the file and repackage the updated file, but depending on your needs thats overkill..

DavidKroukamp
Practically a Master Poster
Team Colleague
693 posts since Dec 2011
Reputation Points: 282
Solved Threads: 169
 

well thank you all for helping me
for the moment I decided to place the text file separated from the jar file
.The text file then placed in and absolute path .
The code can access the text file by knowing the text file's absolute path

thank all
best regard

denny

dennysimon
Junior Poster
114 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You