Java读写修改Property文件
来源:互联网 发布:海南话翻译软件 编辑:程序博客网 时间:2024/06/07 06:13
- package com.test.common;
-
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Date;
- import java.util.Properties;
-
-
-
- public class Data {
-
-
-
-
- private static final String PROPERTY_FILE = "c:/data.properties";
-
-
-
-
-
-
-
- public static String readData(String key) {
- Properties props = new Properties();
- try {
- InputStream in = new BufferedInputStream(new FileInputStream(
- PROPERTY_FILE));
- props.load(in);
- in.close();
- String value = props.getProperty(key);
- return value;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
-
-
-
-
-
-
-
-
- public static void writeData(String key, String value) {
- Properties prop = new Properties();
- try {
- File file = new File(PROPERTY_FILE);
- if (!file.exists())
- file.createNewFile();
- InputStream fis = new FileInputStream(file);
- prop.load(fis);
- fis.close();
- OutputStream fos = new FileOutputStream(PROPERTY_FILE);
- prop.setProperty(key, value);
- prop.store(fos, "Update '" + key + "' value");
- fos.close();
- } catch (IOException e) {
- System.err.println("Visit " + PROPERTY_FILE + " for updating "
- + value + " value error");
- }
- }}