>我有一个问题。prop.getProperty(key)
返回始终为空。 如果我更改属性文件中的某些内容并调用该方法(启动 java 程序(,它会重置值。 我的代码:
public static boolean checkProperties(){
Properties prop = new Properties();
try {
Writer writer = new FileWriter(propertyFile);
Reader reader = new FileReader(propertyFile);
prop.load(reader);
System.out.println(prop.getProperty("server-port")); // OUTPUT: null; But it is in the file!
if(prop.getProperty("server-port") == null || prop.getProperty("server-port").equals("")){
prop.setProperty("server-port", "2424");
System.out.println("No port was specified in the properties file! Set port to 2424.");
}
if(prop.getProperty("info-text") == null || prop.getProperty("info-text").equals("")){
prop.setProperty("info-text", "Welcome to my Game Server!");
System.out.println("No info text was specified in the properties file! Set to 'Welcome to my Game Server!'");
}
prop.store(writer, "Game Server Config");
writer.close();
reader.close();
} catch (IOException e) {
System.err.println("Something went wrong while checking properties file!");
System.err.println(e.getMessage());
e.printStackTrace();
return false;
}
return true;
}
谢谢 Max2002_。
它现在可以工作了。法典:
public static boolean checkProperties(){
Properties prop = new Properties();
try {
//Writer writer = new FileWriter(propertyFile);
FileOutputStream out = null;
Reader reader = new FileReader(propertyFile);
prop.load(reader);
printToConsole(prop.getProperty("server-port"), LEVEL_NORMAL, true);
if(prop.getProperty("server-port") == null || prop.getProperty("server-port").equals("")){
prop.setProperty("server-port", "2424");
printToConsole("No port was specified in the properties file! Set port to 2424.", LEVEL_NORMAL, true);
}
if(prop.getProperty("info-text") == null || prop.getProperty("info-text").equals("")){
prop.setProperty("info-text", "Welcome to my Game Server!");
printToConsole("No info text was specified in the properties file! Set to 'Welcome to my Game Server!'", LEVEL_NORMAL, true);
}
out = new FileOutputStream(propertyFile);
prop.store(out, "Game Server Config");
out.close();
reader.close();
} catch (IOException e) {
printToConsole("Something went wrong while checking properties file!", LEVEL_ERROR, true);
printToConsole(e.getMessage(), LEVEL_ERROR, true);
return false;
}
try{
port = Integer.parseInt(prop.getProperty("server-port"));
}catch(Exception e){
printToConsole("Error: Can not use the port in the properties file ('" + prop.getProperty("server-port") + "')!", LEVEL_ERROR, true);
printToConsole(e.getMessage(), LEVEL_ERROR, true);
return false;
}
return true;
}
试试这个
public static boolean checkProperties() {
Properties prop = new Properties();
InputStream input = null;
boolean flag = false;
try {
String filename = "server.properties";
input = Test.class.getClassLoader().getResourceAsStream(filename);
prop.load(input);
System.out.println(prop.getProperty("server-port"));
if(prop.getProperty("server-port") == null){
flag=false;
}
else{
flag=true;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return flag;
}
如果您遇到任何问题,请告诉我。