• Post Reply Bookmark Topic Watch Topic
  • New Topic

Portable pathnames  RSS feed

 
Marcus Bates
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

I have a config file (actually a properties file) but I cannot package it with my jar file as the users need easy access (via text editor) to modify it.

Can you give me a tip on how to specify its location in a portable way. At the moment I'm doing this (file is in same directory as the jar file).

private static String configFileName = "\\config.properties";
...
String configPathAndFile = System.getProperty("user.dir") +
configFileName;
configProps = new Properties();
try {
// Open the config file.
configFileIs = new FileInputStream(configPathAndFile);
try {
// Load the config properties.
configProps.load(configFileIs);
} catch (IOException ex1) {
// set defaults
}
} catch (FileNotFoundException ex2) {
// set defaults
}
 
Marcus Bates
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, hopefully it is formatted as code now ...

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marcus,
Welcome to the Ranch. We are happy to have you here.

You can always give a relative path.
Imaging the config.properties to be located in a folder called conf which is at the same level as the jar file.
In that case you can specify

Please note while loading loading this from the jar file you will have to use the method.
 
Marcus Bates
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch. We are happy to have you here.

Thanks, glad to be here!

In that case you can specify


I like the idea of the relative path.

My concern is the forward slash, will this work on Windows, UNIX/Linux, etc?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marcus Bates:

My concern is the forward slash, will this work on Windows, UNIX/Linux, etc?


Yup.
Single back slash is used as escape character. So if you want windows style you can '\\' for a single \
Best is to use File.separator. Check out the API docs for File.separator
 
Marcus Bates
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I've changed it as follows:

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!