`
yaerfeng1989
  • 浏览: 226383 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

分享java读写Properties文件

    博客分类:
  • java
阅读更多

Properties用来做配置之类的文件存储,比如数据库配置,连接池配置等。

转载请注明出处:分享java读写Properties文件

package com.zuidaima;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Main {

	public static void main(String[] args) {
		Properties property = new Properties();
		try {
			File file = new File("c:/db.properties");
			if (!file.exists()) {
				file.createNewFile();
			}
			// 写入
			property.setProperty("database", "localhost");
			property.setProperty("user", "zuidaima");
			property.setProperty("password", "password");
			property.store(new FileOutputStream(file), null);

			property.load(new FileInputStream(file));

			// 读取
			System.out.println(property.getProperty("database"));
			System.out.println(property.getProperty("user"));
			System.out.println(property.getProperty("password"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

	    			

 代码下载地址:http://www.zuidaima.com/share/1550463235050496.htm

0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics