您的位置:首页 > 编程语言 > Java开发

java对properties配置文件的读写操作

2015-01-25 15:27 417 查看

1.1. 对properties配置文件的读取

1.1.1. 要读取的文件

配置文件key和value之间用冒号[:]和等于号[=]都是可以的.

Test.properties

name:\u5F20\u4E09
password:\u5BC6\u7801
age:22

1.1.2. 读取的程序代码

PropertiesTest.java

/**
* 读取配置文件
* @throws IOException
*/
@Test
public void read() throws IOException{
Properties properties = new Properties();

File file = new File("test.properties");

FileInputStream fis = new FileInputStream(file);

properties.load(fis);

Set<Object> keySet = properties.keySet();

for (Object object : keySet) {
System.out.println(object.toString()+":"+properties.getProperty(object.toString()));
}
}


1.1.3. 结果

后台打印结果

age:22
password:密码
name:张三

1.2. 对propertiest配置文件的写入操作

1.2.1. 写入程序的代码

PropertiesTest.java

/**
* 向配置文件写入内容
* @throws IOException
*/
@Test
public void write() throws IOException {
Properties properties = new Properties();

String file = "aa.properties";
FileOutputStream fos = new FileOutputStream(file);

properties.setProperty("aa", "123");
properties.setProperty("bb", "456");
properties.setProperty("cc", "789");
properties.setProperty("name", "张三");

properties.store(fos, "保存文件");

fos.close();
}


1.2.2. 结果

aa.propertiest

#\u4FDD\u5B58\u6587\u4EF6
#Sun Jan 25 14:06:25 CST 2015
name=\u5F20\u4E09
aa=123
bb=456
cc=789

1.3. 对properties配置文件的修改操作

1.3.1. 修改前的配置文件

Test.properties

Age=22
Password=密码
Name=张三

1.3.2. 修改程序的代码

PropertiesTest.java

/**
* 对配置文件的修改
* @throws IOException
*/
@Test
public void update() throws IOException{

Properties properties = new Properties();

File file = new File("test.properties");

FileInputStream fis = new FileInputStream(file);

FileOutputStream fos = new FileOutputStream(file);

properties.load(fis);

properties.setProperty("name", "李四");
properties.setProperty("age", "222");
properties.setProperty("age3", "222");
properties.store(fos, "更新配置文件");

fos.close();

fis.close();

}


1.3.3. 修改后的文件内容

Test.properties

#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6
#Sun Jan 25 15:20:40 CST 2015
age=222
name=\u674E\u56DB
password=\u5BC6\u7801
age3=222

1.4. 对properties配置文件的删除

1.4.1. 删除前配置文件的内容

Test.properties

#\u66F4\u65B0\u914D\u7F6E\u6587\u4EF6
#Sun Jan 25 15:20:40 CST 2015
age=222
name=\u674E\u56DB
password=\u5BC6\u7801
age3=222

1.4.2. 删除程序的代码

PropertiesTest.java

/**
* 删除配置文件中的key
* @throws IOException
*
*/
@Test
public void delete() throws IOException{
Properties properties = new Properties();

File file = new File("test.properties");

FileInputStream fis = new FileInputStream(file);

properties.load(fis);

//必须先用map将所有的内容先保存,不然一删除,原来的内容都没了
Map<String, String> map = new HashMap<String, String>();

Set<Object> keySet = properties.keySet();

System.out.println(keySet.size());

for (Object object : keySet) {
String key = (String) object;
String value = (String) properties.get(key);
System.out.println(key+"="+value);
map.put(key, value);
}
//删除key
map.remove("age3");
properties.remove("age3");

for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
properties.setProperty(entry.getKey(), entry.getValue());
}

FileOutputStream fos = new FileOutputStream(file);
properties.store(fos, "删除配置文件中的key:age3");

fos.close();

fis.close();

}


1.4.3. 删除后的配置文件内容

Test.properties

#\u5220\u9664\u914D\u7F6E\u6587\u4EF6\u4E2D\u7684key:age3
#Sun Jan 25 15:23:58 CST 2015
age=222
name=\u674E\u56DB
password=\u5BC6\u7801

1.5. 完整程序代码

PropertieTest.java

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.junit.Test;

public class PropertiesTest {

public static void main(String[] args) {

}
/**
* 删除配置文件中的key
* @throws IOException
*
*/
@Test
public void delete() throws IOException{
Properties properties = new Properties();

File file = new File("test.properties");

FileInputStream fis = new FileInputStream(file);

properties.load(fis);

//必须先用map将所有的内容先保存,不然一删除,原来的内容都没了
Map<String, String> map = new HashMap<String, String>();

Set<Object> keySet = properties.keySet();

System.out.println(keySet.size());

for (Object object : keySet) {
String key = (String) object;
String value = (String) properties.get(key);
System.out.println(key+"="+value);
map.put(key, value);
}
//删除key
map.remove("age3");
properties.remove("age3");

for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
properties.setProperty(entry.getKey(), entry.getValue());
}

FileOutputStream fos = new FileOutputStream(file);
properties.store(fos, "删除配置文件中的key:age3");

fos.close();

fis.close();

}

/**
* 对配置文件的修改
* @throws IOException
*/
@Test
public void update() throws IOException{

Properties properties = new Properties();

File file = new File("test.properties");

FileInputStream fis = new FileInputStream(file);

properties.load(fis);

//必须先用map将所有的内容先保存,不然一更新,原来的内容都没了
Map<String, String> map = new HashMap<String, String>();

Set<Object> keySet = properties.keySet();

System.out.println(keySet.size());

for (Object object : keySet) {
String key = (String) object;
String value = (String) properties.get(key);
System.out.println(key+"="+value);
map.put(key, value);
}

map.put("name", "李四");
map.put("age", "222");
map.put("age3", "222");

for (java.util.Map.Entry<String, String> entry: map.entrySet()) {
properties.setProperty(entry.getKey(), entry.getValue());
}
FileOutputStream fos = new FileOutputStream(file);

properties.store(fos, "更新配置文件");

fos.close();

fis.close();

}

/** * 向配置文件写入内容 * @throws IOException */ @Test public void write() throws IOException { Properties properties = new Properties(); String file = "aa.properties"; FileOutputStream fos = new FileOutputStream(file); properties.setProperty("aa", "123"); properties.setProperty("bb", "456"); properties.setProperty("cc", "789"); properties.setProperty("name", "张三"); properties.store(fos, "保存文件"); fos.close(); }

/**
* 读取配置文件
* @throws IOException
*/
@Test
public void read() throws IOException{
Properties properties = new Properties();

File file = new File("test.properties");

FileInputStream fis = new FileInputStream(file);

properties.load(fis);

Set<Object> keySet = properties.keySet();

for (Object object : keySet) {
System.out.println(object.toString()+"="+properties.getProperty(object.toString()));
}
}

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: