java中Properties文件的读写操作(自动创建文件夹和文件)
来源:互联网 发布:mac虚拟机分辨率设置 编辑:程序博客网 时间:2024/06/05 21:56
/************************************************************************************************/// 静态资源文件路径// String folderPath ="/data/im_cursor";// String filePath = "/im_cursor.properties";String folderPath = "D:/datadata/im_cursor";String filePath = "/im_cursor.properties";/** * 读取属性文件中相应键的值 * * @param key * 主键 * @return String */private String getKeyValue(String key) {// 如果文件夹不存在就创建File folder = new File(folderPath);if (!folder.exists() && !folder.isDirectory()) {System.out.println("//不存在");folder.mkdirs();}// 如果文件不存在就创建File file = new File(folderPath + filePath);if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}Properties props = new Properties();try {// 读取文件FileInputStream fis = new FileInputStream(folderPath + filePath);props.load(fis);} catch (IOException e) {e.printStackTrace();System.exit(-1);}return props.getProperty(key);}/** * 更新properties文件的键值对 如果该主键已经存在,更新该主键的值; 如果该主键不存在,则插件一对键值。 * * @param keyname * 键名 * @param keyvalue * 键值 */private void updateProperties(String keyname, String keyvalue) {// 如果文件夹不存在就创建File folder = new File(folderPath);if (!folder.exists() && !folder.isDirectory()) {System.out.println("//不存在");folder.mkdirs();}// 如果文件不存在就创建File file = new File(folderPath + filePath);if (!file.exists()) {try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}}Properties props = new Properties();try {// 读取文件FileInputStream fis = new FileInputStream(folderPath + filePath);props.load(fis);// 调用 Hashtable 的方法 put,使用 getProperty 方法提供并行性。// 强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。OutputStream fos = new FileOutputStream(folderPath + filePath);props.setProperty(keyname, keyvalue);// 以适合使用 load 方法加载到 Properties 表中的格式,// 将此 Properties 表中的属性列表(键和元素对)写入输出流props.store(fos, "Update '" + keyname + "' value");} catch (IOException e) {System.err.println("属性文件更新错误");}}
0 0
- java中Properties文件的读写操作(自动创建文件夹和文件)
- Android中创建文件夹和文件的操作
- Android中创建文件夹和文件的操作
- 输出文件夹中java文件的路径和文件数目
- python之文件的读写和文件目录以及文件夹的操作
- python之文件的读写和文件目录以及文件夹的操作
- java 创建文件夹和文件
- java中使用文件输入流和文件输出流进行文件读写的用例!
- 流和文件(文件的读写操作)
- 创建文件夹和文件的java代码范例
- 创建文件夹和文件的java代码范例
- SD卡中文件夹和文件的操作
- 在JSP中创建文件夹和文件
- 在JSP中创建文件夹和文件
- 创建文件夹和文件
- java中properties属性文件的读写修改操作
- JAVA中文件夹的复制和文件的遍历
- 创建文件夹和文件的方法
- 移动端webApp兼容问题解决
- 查看UID的方法
- vi/vim键盘图
- 集合(5-有序列表SortedList)
- linux 硬链接 软连接
- java中Properties文件的读写操作(自动创建文件夹和文件)
- MySQL查询语句大全集锦
- Hive 7. 桶 (Bucket)
- 项目管理利器(Maven)——maven介绍及环境搭建
- elasticsearch分布式搜索查询语句(实例)
- Android-点亮屏幕与唤醒
- Maven打包
- Notepad++中设置Windows、Unix、Mac三种行尾换行符格式间的转换
- iOS 比较时间 和 获取当前时间