Java从properties文件读取内容

例如,项目文件布局:
Java从properties文件读取内容_第1张图片

test.properties文件的内容为:

name = thb
pass = 223355

主类文件:

package com.thb;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map.Entry;
import java.util.Properties;

public class Demo4 {

    public static void main(String[] args) {
        String fileName = "src/com/thb/test.properties";
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            Properties pro = new Properties();
            pro.load(in);           
            for (Entryentry : pro.entrySet()) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {            
            e.printStackTrace();
        }
    }

}

运行输出:

pass=223355
name=thb

你可能感兴趣的:(java,开发语言)