Created
October 13, 2013 02:10
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Let's encapsulate the loading of properties in a class:
Let's remove the responsibility of initializing the variables away from
Foo
:To a factory class or main method: