Skip to content

Instantly share code, notes, and snippets.

@gaijinco
Created October 13, 2013 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaijinco/6957285 to your computer and use it in GitHub Desktop.
Save gaijinco/6957285 to your computer and use it in GitHub Desktop.
This is the cleaned version of the code shown here (http://www.drdobbs.com/jvm/readwrite-properties-files-in-java/231000005) to read a Properties file in Java
package org.your_company.your_project.exceptions;
public class ApplicationException extends Exception {
private static final long serialVersionUID = 1L;
private String code;
public ApplicationException(String code) {
this.code = code;
}
public ApplicationException(String code, String message) {
this.code = code;
}
public String getCode() {
return code;
}
}
package org.your_company.your_project;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.your_company.your_project.exceptions.ApplicationException;
public class Foo {
private String serverAddr;
private int serverPort;
private int threadCnt;
public Foo() throws ApplicationException {
loadParams();
}
private void loadParams() throws ApplicationException {
Properties props = getProperties("server.properties");
serverAddr = props.getProperty("ServerAddress", "192.168.0.1");
serverPort = new Integer(props.getProperty("ServerPort", "8080"));
threadCnt = new Integer(props.getProperty("ThreadCount", "5"));
}
private Properties getProperties(String path) throws ApplicationException {
try {
return doGetProperties(path);
} catch (FileNotFoundException e) {
throw new ApplicationException("message.file_not_found");
} catch (IOException e) {
throw new ApplicationException("message.io.error", e.getMessage());
}
}
private Properties doGetProperties(String path)
throws FileNotFoundException, IOException {
Properties props = new Properties();
InputStream is = getInputStream(path);
props.load(is);
return props;
}
private InputStream getInputStream(String path)
throws FileNotFoundException {
try {
return getInputStreamFromFileSystem(path);
} catch (FileNotFoundException e) {
return getInputStreamFromClasspath(path);
}
}
private InputStream getInputStreamFromFileSystem(String path)
throws FileNotFoundException {
File f = new File("server.properties");
InputStream is = new FileInputStream(f);
return is;
}
private InputStream getInputStreamFromClasspath(String path)
throws FileNotFoundException {
InputStream is = getClass().getResourceAsStream("server.properties");
if (is == null) {
throw new FileNotFoundException();
}
return is;
}
}
package org.your_company.your_project;
import org.your_company.your_project.exceptions.ApplicationException;
public class Main {
public static void main(String[] args) throws ApplicationException {
long start = System.nanoTime();
Foo foo = new Foo();
long end = System.nanoTime();
System.out.println(end - start);
}
}
@germanescobar
Copy link

Let's encapsulate the loading of properties in a class:

public class PropertiesLoader {
  // notice that all methods are public because they can be useful and can be individually tested  

  public static Properties loadPropertiesFromFileSystemOrClassPath(String path) throws FileNotFoundException, IOException {
    try {
      return loadPropertiesFromFileSystem(path);
    } catch (FileNotFoundException e) {
      return loadPropertiesFromClassPath(path);
    }
  }

  public static Properties loadPropertiesFromFileSystem(String path) throws FileNotFoundException, IOException {
    return loadPropertiesFromInputStream(new FileInputStream(path));
  }

  public static Properties loadPropertiesFromClassPath(String path) throws FileNotFoundException, IOException {
    InputStream inputStream = Thread.getContextClassLoader().getResourceAsStream(path);
    if (inputStream == null) {
      throw new FileNotFoundException("...");
    } else {
      return loadPropertiesFromInputStream(inputStream);
    }
  }

  public static Properties loadPropertiesFromInputStream(InputStream inputStream) {
    Properties properties = new Properties();
    properties.load(inputStream);
    return properties;
  }

}

Let's remove the responsibility of initializing the variables away from Foo:

public class Foo {
   private String serverAddress;
   private int port;
   private int threads;

   // getter, setters
}

To a factory class or main method:

Properties properties = PropertiesLoader.loadPropertiesFromFileSystemOrClassPath("server.properties");
Foo foo = new Foo();
foo.setServerAddress(properties.getProperty("..."));  
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment