廣告

2015年4月13日 星期一

[java] read/write properties file | 讀/寫 properties 檔


package testJava;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;

public class testProperties {

 private static String propertiesName = "server.properties";
 private static String serverAddr  = "";
 private static Integer serverPort = 0;
 private static Integer threadCnt  = 0;
 
 
 public static void main(String[] args) {
  
  System.out.println("before load Properties");
  System.out.println("serverAddr:"+serverAddr+"\nserverPort:"+serverPort+"\nthreadCnt:"+threadCnt);
  System.out.println("after load Properties");
  loadParams();
  System.out.println("serverAddr:"+serverAddr+"\nserverPort:"+serverPort+"\nthreadCnt:"+threadCnt);
  System.out.println("==================");
  System.out.println("save value to Properties");
  System.out.println("serverAddr:"+serverAddr+"\nserverPort:"+serverPort+"\nthreadCnt:"+threadCnt);
  serverAddr = "192.168.0.0";
  serverPort = 4543;
  threadCnt  = 111;
  saveParamChanges();
  System.out.println("load value form Properties");
  loadParams();
  System.out.println("serverAddr:"+serverAddr+"\nserverPort:"+serverPort+"\nthreadCnt:"+threadCnt);
  System.out.println("==================");
  System.out.println("print all content");
  printThemAll();
 }

 public static  void loadParams() {
     Properties props = new Properties();
     InputStream is = null;
  
     // First try loading from the current directory
  try {
   is = new FileInputStream(propertiesName);
   props.load(is);
   
      serverAddr = props.getProperty("ServerAddress");
      serverPort = new Integer(props.getProperty("ServerPort"));
      threadCnt  = new Integer(props.getProperty("ThreadCount")); 
      
      
  } catch (Exception e) {
   System.out.println(e.getMessage().toString());
  }finally {
   if (is != null) {
    try {
     is.close();
    } catch (Exception e) {
     System.out.println(e.getMessage().toString());
    }
   }
  }

 }
 
 public static void saveParamChanges() {
  OutputStream out = null;
  Properties props = new Properties();
  try {
         props.setProperty("ServerAddress", serverAddr);
         props.setProperty("ServerPort", ""+serverPort);
         props.setProperty("ThreadCount", ""+threadCnt);
         out = new FileOutputStream(propertiesName);
         props.store(out, "This is an optional header comment string");
     }
     catch (Exception e ) {
      System.out.println(e.getMessage().toString());
     }finally {
   if (out != null) {
    try {
     out.close();
    } catch (Exception e) {
     System.out.println(e.getMessage().toString());
    }
   }
  
  }
 }
 
 
 private static void printThemAll() {
   
  Properties prop = new Properties();
  InputStream input = null;
  
  try {
  
   String filename = propertiesName;
   
   input = new FileInputStream(propertiesName);
   prop.load(input);
   
   if (input == null) {
    System.out.println("Sorry, unable to find " + filename);
    return;
   }
   
   Enumeration e = prop.propertyNames();
   while (e.hasMoreElements()) {
    String key = (String) e.nextElement();
    String value = prop.getProperty(key);
    System.out.println("Key : " + key + ", Value : " + value);
   }
  
  } catch (Exception e) {
   System.out.println(e.getMessage().toString());
  } finally {
   if (input != null) {
    try {
     input.close();
    } catch (IOException e) {
     System.out.println(e.getMessage().toString());
    }
   }
  }
  
   }

}



console
before load Properties
serverAddr:
serverPort:0
threadCnt:0
after load Properties
server.properties (系統找不到指定的檔案。)
serverAddr:
serverPort:0
threadCnt:0
==================
save value to Properties
serverAddr:
serverPort:0
threadCnt:0
load value form Properties
serverAddr:192.168.0.0
serverPort:4543
threadCnt:111
==================
print all content
Key : ServerPort, Value : 4543
Key : ServerAddress, Value : 192.168.0.0
Key : ThreadCount, Value : 111


沒有留言:

張貼留言