0

i am developing a java application. I've got a txt file such like this:

Component(Journal, Account, AnalysisCodes...):
Journal
*****
Method(Query, Import, CreateOrAmend...):
Import
*****
Username:
PKP
*****
Password:
test
*****
Host:
localhost
*****
Port:
8080
*****
Program's path:
C:/Connect Manager/
*****
XML Name:
ledger.xml
*****

This java program does not write anything in this file, it just reads it. However i could not figure out where to store this txt file. Path must be same for every users since it is hard coded inside. I cannot read path of the program if i don't know the path of program.

Here is my code:

String lineconfig="";
String pathconfig= "C:/Connect Manager/" + "config.txt";
BufferedReader readerconfig= new BufferedReader(new FileReader(pathconfig));
while((lineconfig=readerconfig.readLine())!=null)
{
    stringList.add(lineconfig);
}
readerconfig.close();

As you see, i need an exact location for pathconfig.

Sorry if i caused any confusion in question. I would appreciate your suggestions.

Besides that: Another program should be able to edit this or the user. I will make this program a jar.

4
  • Put this file in your project and set pathconfig just as config.txt. Commented Aug 23, 2013 at 12:25
  • MyClass.class.getResourceAsStream("config.txt"); then put it in same level as your class. Or, you could use System.getProperty("user.dir") Commented Aug 23, 2013 at 12:25
  • 2
    Not directly related to the question but might be though useful for the OP: Have a look at Property Files Commented Aug 23, 2013 at 12:28
  • By the time of deployment, those resources will likely become an embedded-resource. That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL. Commented Aug 23, 2013 at 17:20

3 Answers 3

4

I would suggest you use a properties file. It is better to read and it is easier for the user to configure the properties. Look at this short tutorial for an explanation of the .properties file. To read and write values from and to the file follow this short tutorial.

Have you tried using:

path = "config.txt"

If you use this syntax you should get the file in the same directory as the jar. Keep in mind that you can use the shortcuts ".." and "." to go to a directory.

With .. you go to the directory above.

With . you get your actual directory.

Sign up to request clarification or add additional context in comments.

2 Comments

It looks like this tutorial is going to work. Thanks for answer!
Your welcome ^^ Look at the update with the definitions of how to use the propertie file and the tutorial.
4

I'd use properties and you can place them in your res folder. Check out this example for more.

public class App 
{
    public static void main( String[] args )
    {
        Properties prop = new Properties();

        try {
          //load a properties file from class path, inside static method
          prop.load(App.class.getClassLoader().getResourceAsStream("config.properties");));

          //get the property value and print it out
          System.out.println(prop.getProperty("database"));
          System.out.println(prop.getProperty("dbuser"));
          System.out.println(prop.getProperty("dbpassword"));

        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }
}

The res folder is part of your project, so it's (somehow) always in the same place.

Comments

0

I recommend you to have a look at the Java Preferences API:

http://docs.oracle.com/javase/7/docs/api/java/util/prefs/Preferences.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.