java修改读取properties配置文件中的内容
2013-03-07 16:28
651 查看
java修改properties配置文件中的内容
读取properties配置文件中的内容
oao.properties内容
/** * 修改properties配置文件中data_time的值 * @param bigtime 开始时间 * @param endtime 结束时间 * @throws IOException */ public static void getProperties(String bigtime,String endtime) throws IOException { String path = Thread.currentThread().getContextClassLoader() .getResource("").getPath().substring(1); FileInputStream fis = new FileInputStream(path + "classes/email.properties");// 属性文件输入流 byte[] buf = new byte[fis.available()]; StringBuffer sb = new StringBuffer(); while ((fis.read(buf)) != -1) { sb.append(new String(buf)); buf = new byte[1024];// 重新生成,避免和上次读取的数据重复 } //得到修改后的字符串 StringBuffer pr = sb.replace(sb.indexOf("data_time")+10, sb.indexOf("#数据"), bigtime+"-"+endtime+"\r\n"); fis.close();// 关闭流 // 文件输出流 FileOutputStream fos = new FileOutputStream(path + "classes/email.properties"); // 将Properties集合保存到流中 fos.write(pr.toString().getBytes()); fos.close();// 关闭流 }
读取properties配置文件中的内容
public class PropertiesUtils { private static final String LOCATION = "oao.properties"; private static Properties properties = new Properties(); /** * 根据KEY获取参数 * * @param key * @return */ public static String getProperties(String key) { String result = null; InputStream input = null; try { input = PropertiesUtils.class.getClassLoader().getResourceAsStream(LOCATION); properties.load(input); result = properties.getProperty(key); } catch (IOException e) { e.printStackTrace(); result = ""; } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); result = ""; } } } return result; } public static void main(String[] args) { System.out.println(getProperties("ACCOUNT_PAGE_SIZE")); } }
oao.properties内容
ACCOUNT_PAGE_SIZE=20
相关文章推荐
- 关于java读取和写入properties配置文件的内容
- java 如何从配置文件(.properties)中读取内容
- Java Properties 类读取和修改配置文件信息
- java 读取,修改properties文件,不改变文件内容顺序
- java读取resource下xxx.properties配置文件内容
- Java中读取properties属性配置文件内容方法
- java 读取properties配置文件内容乱码 --日文乱码对应方法
- Java Properties 类读取和修改配置文件信息
- Java Properties 类读取和修改配置文件信息
- java.util.Properties读取中文内容的配置文件,发生中文乱码的现象有解决方案
- java中读取properties配置文件中的内容的三种方式
- Java中获取包含变量的配置文件config.properties内容
- java 数据库读取工具类(读取config.properties配置文件)
- java读取.properties配置文件的几种方法
- Java开发中读取XML与properties配置文件的方法
- Java读取WEB-INF目录下的properties配置文件
- JAVA读取Properties的六种方法--配置文件
- Java配置文件Properties的读取、写入与更新操作
- javaweb读取任意目录的下的properties配置文件(解决普通java类读web-inf下任意目录)
- Java中读取配置文件(properties、xml)