How to load files on multiple platforms properly using Java?

13,245

Solution 1

If the file is in a jar file and accessed by the classpath then you should always use /.

The JavaDocs for the ClassLoader.getResource say that "The name of a resource is a '/'-separated path name that identifies the resource."

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)

Solution 2

I'm not sure if there is the proper way, but one way is:

File confDir = new File("conf");
File propFile = new File(confDir, "properties.xml");

But in a scenario as simple as yours, I would just use /

Share:
13,245
Harsha
Author by

Harsha

I am a BCS post graduate level student and working as a java programmer in Kandy, Sri Lanka. I've fallen love with java so much.

Updated on July 13, 2022

Comments

  • Harsha
    Harsha almost 2 years

    I have a java swing database application which needs to be run on Windows and Linux. My database connection details are stored in a XML file and I load them.

    This application can load this properties on Linux properly but it is not working on Windows.

    How do I load files on multiple platforms properly using Java?


    This is the code:

    PropertyHandler propertyWriter = new PropertyHandler();
    
    List keys = new ArrayList();
    keys.add("ip");
    keys.add("database");
    Map localProps = propertyWriter.read(keys, "conf" + File.separatorChar + "properties.xml", true);//if false load from the local properties
    
    //get properties from the xml in the internal package
    List seKeys = new ArrayList();
    seKeys.add("driver");
    seKeys.add("username");
    seKeys.add("password");
    
    Map seProps = propertyWriter.read(seKeys, "conf" + File.separatorChar + "properties.xml", true);
    
    String dsn = "jdbc:mysql://" + (String) localProps.get("ip") + ":3306/" + (String) localProps.get("database");
    jDBCConnectionPool = new JDBCConnectionPool((String) seProps.get("driver"), dsn, (String) seProps.get("username"), (String) seProps.get("password"));
    

    File reader method:

    public Map read(List properties, String path, boolean isConfFromClassPath)
    {
        Properties prop = new Properties();
        Map props = new HashMap();
        try {
    
            if (isConfFromClassPath) {
                InputStream in = this.getClass().getClassLoader().getResourceAsStream(path);
                prop.loadFromXML(in);
    
                for (Iterator i = properties.iterator(); i.hasNext();) {
                    String key = (String) i.next();
                    props.put(key, prop.getProperty(key));
                }
                in.close();
    
            } else {
                FileInputStream in = new FileInputStream(path);
                prop.loadFromXML(in);
    
                for (Iterator i = properties.iterator(); i.hasNext();) {
                    String key = (String) i.next();
                    props.put(key, prop.getProperty(key));
                }
                in.close();
            }
    
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return props;
    }