-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.java
More file actions
52 lines (40 loc) · 1.18 KB
/
Config.java
File metadata and controls
52 lines (40 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// This class reads in a configuration file, called "config.txt", and makes
// its contents easily available to Java code.
//
// A configuration entry can be read as a String, like this:
//
// String str = Config.getAsString("entryname");
//
// An entry can be read as an integer, like this:
//
// int i = Config.getAsInt("entryname");
//
// For documentation on the format of the config.txt file, see that file.
import java.io.FileInputStream;
import java.util.Properties;
import java.io.IOException;
public class Config {
private static String configFileName = "config.txt";
private static boolean needInit = true;
private static Properties props;
private static void init() {
needInit = false;
try{
FileInputStream fis = new FileInputStream(configFileName);
if(fis != null){
props = new Properties();
props.load(fis);
}
}catch(IOException x){
x.printStackTrace(System.err);
}
}
public static String getAsString(String name) {
if(needInit) init();
return props.getProperty(name);
}
public static int getAsInt(String name) {
if(needInit) init();
return Integer.parseInt(getAsString(name));
}
}