oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/java/exceptions.txt
2.0 KiB, 1 run
created by r1870400018:11988, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | import java.io.BufferedReader; |
| 2 | import java.io.FileReader; |
| 3 | import java.io.IOException; |
| 4 | import java.util.Properties; |
| 5 | |
| 6 | public class ConfigLoader { |
| 7 | |
| 8 | private final Properties props = new Properties(); |
| 9 | |
| 10 | public void load(String path) throws IOException { |
| 11 | try (BufferedReader reader = new BufferedReader(new FileReader(path))) { |
| 12 | props.load(reader); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | public String getString(String key) throws ConfigException { |
| 17 | String value = props.getProperty(key); |
| 18 | if (value == null) { |
| 19 | throw new ConfigException("missing key: " + key); |
| 20 | } |
| 21 | return value; |
| 22 | } |
| 23 | |
| 24 | public int getInt(String key) throws ConfigException { |
| 25 | String value = getString(key); |
| 26 | try { |
| 27 | return Integer.parseInt(value); |
| 28 | } catch (NumberFormatException e) { |
| 29 | throw new ConfigException( |
| 30 | String.format("key '%s' is not an integer: %s", key, value), e); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public boolean getBoolean(String key, boolean defaultValue) { |
| 35 | String value = props.getProperty(key); |
| 36 | if (value == null) { |
| 37 | return defaultValue; |
| 38 | } |
| 39 | return Boolean.parseBoolean(value); |
| 40 | } |
| 41 | |
| 42 | public static class ConfigException extends Exception { |
| 43 | public ConfigException(String message) { |
| 44 | super(message); |
| 45 | } |
| 46 | |
| 47 | public ConfigException(String message, Throwable cause) { |
| 48 | super(message, cause); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | public static void main(String[] args) { |
| 53 | ConfigLoader loader = new ConfigLoader(); |
| 54 | try { |
| 55 | loader.load("app.properties"); |
| 56 | String host = loader.getString("db.host"); |
| 57 | int port = loader.getInt("db.port"); |
| 58 | boolean ssl = loader.getBoolean("db.ssl", false); |
| 59 | System.out.printf("db: %s:%d (ssl=%b)%n", host, port, ssl); |
| 60 | } catch (IOException e) { |
| 61 | System.err.println("cannot read config: " + e.getMessage()); |
| 62 | } catch (ConfigException e) { |
| 63 | System.err.println("config error: " + e.getMessage()); |
| 64 | } |
| 65 | } |
| 66 | } |