dangsn
2024-12-26 4e9ff2ce6a830bb2340d7c8612c72eea0c5a553e
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
92
93
94
package com.vci.client.logon.client;
 
 
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
 
public class GetOrSetProperty {
    
    public static String readValue(String filePath, String key) {
        String value = null;
        if (key == null) {
            return null;
        } 
        Properties props = new Properties();
        try {
            InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
            props.load(ips);
            value = props.getProperty(key);
        //    System.out.println(key + "=" + value);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            value = null;
        } catch (IOException e) {
            e.printStackTrace();
            value = null;
        } catch (Exception e) {
            e.printStackTrace();
            value = null;
        }
        return value;
    }
    
    public static String[] readValues(String filePath, String[] keys) {
        String[] values = null;
        if (keys == null) {
            return null;
        }
        Properties props = new Properties();
        try {
            InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
            props.load(ips);
            int len = keys.length;
            values = new String[len];
            for (int i = 0; i < len; i++) {
                values[i] = props.getProperty(keys[i]);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            values = null;
        } catch (IOException e) {
            e.printStackTrace();
            values = null;
        } catch (Exception e) {
            e.printStackTrace();
            values = null;
        }
        return values;
    }
    
    public static void writeProperties(String filePath, Hashtable<String, String> ht) {
        Properties props = new Properties();
        try {
            Enumeration<String> enumration = ht.keys();
            OutputStream ops = new FileOutputStream(filePath);
            while (enumration.hasMoreElements()) {
                String key = enumration.nextElement();
                String value = ht.get(key);
                props.setProperty(key, value);
            }
            props.store(ops, "set");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void writeProperties(String filePath, String paraKey, String paraValue) {
        Properties props = new Properties();
        try {
            OutputStream ops = new FileOutputStream(filePath);
            props.setProperty(paraKey, paraValue);
            props.store(ops, "set");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}