`

读取properties文件

    博客分类:
  • Java
 
阅读更多

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


public class ReadFile {

    private String name;
   
    private String password;
   
    private String driver;
   
    private String url;
    /**
     * @param args
     */
    public static void main(String[] args) {
        ReadFile rf = new ReadFile();
        rf.getKey();
    }
   
    public void getKey(){
        Properties prop = new Properties();
        //InputStream ins = this.getClass().getClassLoader().getResourceAsStream("prop.properties");//读取properties文件的第一种方法
        try {
            InputStream ins2 = new BufferedInputStream(new FileInputStream("D:/workspace2/test/src/prop.properties"));//读取properties文件的第二种方法
            prop.load(ins2);
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        this.name = prop.getProperty("name");
        this.password = prop.getProperty("password");
        this.driver = prop.getProperty("driver");
        this.url = prop.getProperty("url");
       
        System.out.println(name);
        System.out.println(password);
        System.out.println(driver);
        System.out.println(url);
       
    }

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics