2015年10月2日 星期五

Properties 的使用


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class Demo1 {
    public static void main(String[] args) throws FileNotFoundException{
    
        Properties myProps = new Properties();
        try{
            FileInputStream fis = new FileInputStream("C:\\ServerInfo.properties");
            myProps.load(fis);
        } catch (IOException e){
            System.out.println("Error: " + e.getMessage());
        }
        
        System.out.println("Server: " + myProps.getProperty("hostName"));
        System.out.println("User: " + myProps.getProperty("userName"));
        System.out.println("Password: " + myProps.getProperty("password"));
           
    }
}

Properties 的檔案內容:
hostName=www.example.com
userName=user
password=pass