• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Portable pathnames

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, hopefully it is formatted as code now ...

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I've changed it as follows:

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic