关闭

资源文件操作

69人阅读 评论(0) 收藏 举报
分类:
package com.kanq.platform.view.web.controller;


import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;


import javax.servlet.http.HttpServletRequest;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
@RequestMapping("/operProperties")
public class OperProperties {

@RequestMapping("/test")
public void test(HttpServletRequest request) throws IOException{
// System.out.println(request.getSession().getServletContext().getRealPath("/"));//工程所在目录
// System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());
String filePath=Thread.currentThread().getContextClassLoader().getResource("/properties/jdbc2.properties").getPath();
// new OperProperties().GetValueByKey(filePath, "jdbc.url");
// new OperProperties().GetAllProperties(filePath);
// new OperProperties().writePropertiesFile(filePath);
new OperProperties().WriteProperties(filePath, "name", "wgy111test");
}
/**
* 写入资源文件
* @param filePath
* @param pKey
* @param pValue
* @throws IOException
*/
public void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
Properties pps = new Properties();
InputStream in = new FileInputStream(filePath);
pps.load(in);
OutputStream out = new FileOutputStream(filePath);
pps.setProperty(pKey, pValue);
pps.store(out, "Update " + pKey + " name");
}
// public void writePropertiesFile(String filename) throws IOException{
// Properties properties = new Properties();
// OutputStream outputStream = new FileOutputStream(filename);
// properties.setProperty("username", "myname");
// properties.setProperty("password", "mypassword");
// properties.setProperty("chinese", "1111111111111");
// properties.store(outputStream, "author: shixing_11@sina.com");
// outputStream.close();
// }
 
/**
* 根据Key读取Value
* @param filePath
* @param key
* @return
*/
public String GetValueByKey(String filePath, String key) {
Properties pps = new Properties();
try {
InputStream in = new BufferedInputStream (new FileInputStream(filePath));
pps.load(in);
String value = pps.getProperty(key);
System.out.println(key + " = " + value);
return value;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 读取Properties的全部信息
* @param filePath
* @throws IOException
*/
public void GetAllProperties(String filePath) throws IOException {
Properties pps = new Properties();
InputStream in = new BufferedInputStream(new FileInputStream(filePath));
pps.load(in);
Enumeration en = pps.propertyNames(); //得到配置文件的名字
while(en.hasMoreElements()) {
String strKey = (String) en.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
}
}
public static void main(String[] args) {
System.out.println("==========");
// System.out.println(System.getProperties("jdbc.properties"));
}


}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场