修改Properties
Posted java-h
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了修改Properties相关的知识,希望对你有一定的参考价值。
package com.jm.label.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 修改Properties
* @author win 10
*
*/
public class PropertiesUtils {
/**
* 修改文件内容
* @param store_name
* @param mend_text
* @param importprf
* @throws IOException
*/
public static void modification(String store_name,String mend_text,String importprf) {
try {
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(importprf);
//将流中的数据加载进集合
prop.load(fis);
prop.setProperty(store_name , mend_text);
FileOutputStream fos = new FileOutputStream(importprf);
prop.store(fos,"modification");
fos.close();
fis.close();
} catch (IOException e) {
System.err.println("PropertiesUtils错误");
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
}
//prop.list(System.out);
}
/**
* 添加文件内容
* @param store_name
* @param mend_textadd
* @param importprf
* @throws IOException
*/
public static void add(String store_name,String mend_text,String importprf) {
Properties prop = new Properties();
FileInputStream fis;
FileOutputStream fos;
try {
fis = new FileInputStream(importprf);
//将流中的数据加载进集合
prop.load(fis);
prop.setProperty(store_name , mend_text);
fos = new FileOutputStream(importprf);
prop.store(fos,"add");
//prop.list(System.out);
fos.close();
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 读取文件内容
* @param store_name
* @param importprf
* @return
*/
public static String show(String store_name,String importprf) {
Properties prop = new Properties();
try {
InputStream in = new FileInputStream(new File(importprf));
prop.load(in);
} catch (IOException e) {
System.err.println("PropertiesUtils错误");
// TODO Auto-generated catch block
e.printStackTrace();
}
return prop.getProperty(store_name);
}
public static void main(String[] args) throws IOException {
modification("FileName", "tes123t456", "time.properties");
add("Sql", "jm123456", "time.properties");
show("FileName", "time.properties");
show("Sql", "time.properties");
}
}
以上是关于修改Properties的主要内容,如果未能解决你的问题,请参考以下文章