资源文件操作
来源:互联网 发布:笔记软件 知乎 编辑:程序博客网 时间:2024/06/07 03:14
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"));
}
}
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
- 操作资源文件
- Spring文件资源操作
- 资源文件操作总结
- 资源文件操作
- ASP.NET操作资源文件
- java资源文件操作类
- 安卓--操作资源文件
- c#:操作resx资源文件
- 关于C#资源文件操作的总结
- 资源文件的操作结合反射
- C#对资源文件的操作
- 操作项目中的资源文件-*.properties实例
- Android资源文件在配置文件中的使用与java代码对资源文件的操作
- VS下对Resx资源文件的操作
- Spring(1)——文件资源操作
- api实现对其它exe文件写入标准资源操作
- 需要这样一个记录本-4 C#资源文件操作示例
- Android开发中关于资源文件的存储操作
- 系统、程序的泛泛理解
- NanoApe Loves Sequence Ⅱ
- Android Studio使用技巧
- 深度学习算法实践12---卷积神经网络(CNN)实现
- JavaScript调试技巧之console.log()详解
- 资源文件操作
- 专门为了面试而学的 Java
- 定时删除旧日志
- WebView Cache 缓存清除
- 基于Flume的美团日志收集系统(一)架构和设计
- opencv 取Mat元素的值
- redis两种持久化策略
- 老教授语
- Android开发之App字体大小不随着系统字体大小的改变而改变