医院废弃物信息化管理平台
“mitao”
2025-12-24 3763145f34d7706923d55edc32f5cfbc8bc08b92
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package cn.besteam.common.util;
 
import java.io.*;
import java.util.Properties;
 
//properties文件生成和读写
public class propertiesUtil {
    // 静态资源文件路径
    // String folderPath ="/data/im_cursor";
    // String filePath = "/im_cursor.properties";
    static String folderPath = "e:/";
    static String filePath = "/medicalSdk.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 static 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("属性文件更新错误");
        }
    }
    public static void main(String[] args) {
        updateProperties("equipmentCode","123");
    }
}