Рабочий пример java.util.Properties
Ух, и довольно ж долго я с ним провозился. Как-то не легко мне далось это знание, да и само оно какое-то размытое, но по порядку.
Итак, возникла задача в нашем реминдере сделать запоминание настроек, своего городить не хотелось, решил погуглить. Нагуглил волшебный класс java.util.Properties. Ну и начал его использовать. Да вот только порядок сздания потоков что то все забывают указать. Короче вот тут выкладываю рабочий примерчик, что бы не было у остальных проблем.
Файл Main.java
import java.io.*;
import java.util.*;
/**
* User: dimka3210
* Date: 10.03.13
* Time: 10:20
*/
public class Main {
public static void main(String args[]) throws IOException {
String filePath = "property.ini";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
Properties properties = new Properties();
HashMap<String, String> arraySettings = new HashMap<String, String>();
try {
fileInputStream = new FileInputStream(filePath);
properties.load(fileInputStream);
Enumeration enumeration = properties.keys();
//properties.setProperty("test", "test");
while (enumeration.hasMoreElements()){
String key = enumeration.nextElement().toString();
arraySettings.put(key, properties.getProperty(key));
if (arraySettings.get(key).equals("test")){
arraySettings.remove("test");
}
}
System.out.println(arraySettings);
fileOutputStream = new FileOutputStream(filePath);
properties.store(fileOutputStream, "no comment");
} catch (FileNotFoundException e) {
new File("property.ini").createNewFile();
System.out.println("createNewFile");
e.getMessage();
} catch (InvalidPropertiesFormatException e) {
e.getMessage();
} catch (IOException e) {
e.getMessage();
} finally {
fileOutputStream.close();
fileInputStream.close();
}
}
}
Комментарии:
Нету комментариев для вывода...