JAva propertiesプロファイルの読み込み内容の変更

2003 ワード

JAva 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
"); 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