目录
了解java.io.Properties类常用的方法。
使用到的常量
- static Properties properties = new Properties();
- static {
- properties.put("name", "张三");
- properties.put("age", "11");
- properties.put("sex", "男");
- }
- public static final String MY_PROJECT_FILE = "a/a.properties";
- public static final String MY_PROJECT_FILE_XML = "a/a.xml";
- public void traversal() {
- //获取属性名称
- Set<String> set = properties.stringPropertyNames();
- System.out.println(set);
- //根据属性名称遍历属性值
- for (String s : set) {
- System.out.println(properties.getProperty(s));
- }
- }
- public void getDefaultValue() {
- //获取属性名称
- Set<String> set = properties.stringPropertyNames();
- System.out.println(set);
- System.out.println(properties.getProperty("id", "123456"));
- }
- /**
- * 将Properties的属性写入文件
- * 这里用到了输出流
- */
- public void writeFileFromProperties() {
- PrintStream printStream = null;
- PrintWriter printWriter = null;
- FileOutputStream fileOutputStream = null;
- try {
- //方法一:
- printStream = new PrintStream(MY_PROJECT_FILE);
- properties.list(printStream);
- //方法二:
- printWriter = new PrintWriter(MY_PROJECT_FILE);
- properties.list(printWriter);
- //方法三:
- fileOutputStream = new FileOutputStream(MY_PROJECT_FILE);
- properties.store(fileOutputStream, "方法三");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (printStream != null) {
- printStream.close();
- }
- if (printWriter != null) {
- printWriter.close();
- }
- if (fileOutputStream != null) {
- try {
- fileOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * 读取文件内容返回Properties对象
- * 这里用到了输入流
- */
- public void readFileToProperties() {
- FileReader fileReader = null;
- try {
- fileReader = new FileReader(MY_PROJECT_FILE);
- Properties p = new Properties();
- p.load(fileReader);
- System.out.println(p);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fileReader != null) {
- fileReader.close();
- }
-
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 读取xml文件的所有属性返回Properties对象
- * 这里用到了输入流
- */
- public void readFileToXML() {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(MY_PROJECT_FILE_XML);
- Properties p = new Properties();
- p.loadFromXML(fis);
- System.out.println(p);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (fis != null) {
- fis.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 将Properties的属性写入XML文件
- * 这里用到了输出流
- */
- public void writeXMLFromProperties() {
- FileOutputStream fileOutputStream = null;
- try {
- //写入xml内容
- fileOutputStream = new FileOutputStream(MY_PROJECT_FILE_XML);
- properties.storeToXML(fileOutputStream, "写入xml内容", "UTF-8");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (fileOutputStream != null) {
- try {
- fileOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }