1 package com.epichust.mestar.client.utils;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.util.Properties;
8
9 /**
10 * 读取配置文件工具类
11 *
12 *
13 */
14 public class PropertiesParser
15 {
16 public PropertiesParser()
17 {
18 }
19
20 /**
21 * 获取指定KEY的值
22 *
23 * @param path
24 * @param key
25 * @return
26 * @throws Exception
27 */
28 public static String getProperty(File file, String key) throws Exception
29 {
30 Properties properties = new Properties();
31 String value = "";
32 FileInputStream in = new FileInputStream(file);
33 properties.load(in);
34 value = properties.getProperty(key);
35 in.close();
36
37 return value;
38 }
39
40 public static String getProperty(String filename, String key)
41 {
42 Properties properties = new Properties();
43 String result = "";
44 try
45 {
46 String path = System.getProperty("user.dir");
47 File file = new File(path, filename);
48 FileInputStream in = new FileInputStream(file);
49 properties.load(in);
50 result = properties.getProperty(key);
51 in.close();
52 }
53 catch (IOException e)
54 {
55 e.printStackTrace();
56
57 }
58 return result;
59
60 }
61
62 public static void setProperty(String filename, String key, String value)
63 {
64 Properties properties = new Properties();
65 try
66 {
67
68 String path = System.getProperty("user.dir");
69 File infile = new File(path, filename);
70 FileInputStream in = new FileInputStream(infile);
71 properties.load(in);
72 in.close();
73 if (properties.containsKey(key))
74 {
75 properties.setProperty(key, value);
76 }
77 else
78 {
79 properties.setProperty(key, value);
80 }
81
82 File outfile = new File(path, filename);
83 FileOutputStream output = new FileOutputStream(outfile);
84 properties.store(output, null);
85 output.flush();
86 output.close();
87
88 }
89 catch (IOException e)
90 {
91 e.printStackTrace();
92
93 }
94 }
95
96
97 public static Properties getFunctionMenuProperties()
98 {
99 String path = System.getProperty("user.dir");
100 Properties properties = new Properties();
101 try
102 {
103 properties.load(new FileInputStream(new File(path,"functionMenuConfig.properties")));
104 } catch (IOException e)
105 {
106 e.printStackTrace();
107 }
108 return properties;
109 }
110
111 public static void saveMenuConfig(Properties properties)
112 {
113 String path = System.getProperty("user.dir");
114 FileOutputStream output;
115 try
116 {
117 output = new FileOutputStream(new File(path,"functionMenuConfig.properties"));
118 properties.store(output, null);
119 output.flush();
120 output.close();
121 } catch (Exception e)
122 {
123 e.printStackTrace();
124 }
125
126 }
127
128 public static void main(String args[])
129 {
130
131 int StringCount = 21;
132
133 int[][] position = new int[StringCount][2];
134 int[] fontSize = new int[StringCount];
135
136 String filename = "print.properties";
137 String tempPosition = PropertiesParser.getProperty(filename, "fontPosition");
138 String tempSize = PropertiesParser.getProperty(filename, "fontSize");
139 System.out.println(tempPosition);
140 System.out.println(tempSize);
141 String[] positionSet = tempPosition.split(",");
142 String[] sizeSet = tempSize.split(",");
143
144 if (positionSet.length == 42 && sizeSet.length == StringCount)
145 for (int i = 0; i < StringCount; i++)
146 {
147 position[i][0] = new Integer(positionSet[i * 2].trim());
148 position[i][1] = new Integer(positionSet[i * 2 + 1].trim());
149
150 fontSize[i] = new Integer(sizeSet[i].trim());
151 }
152 else
153 {
154 }
155
156 for (int j = 0; j < fontSize.length; j++)
157 {
158 System.out.print(" [" + position[j][0] + ", " + position[j][1] + "] ");
159 System.out.println("(" + fontSize[j] + ")");
160 }
161
162 // PropertiesParser.setProperty(filename, "a", ":i was testing ");
163 // System.out.println(PropertiesParser.getProperty(filename, "a"));
164
165 }
166
167 }
高质量的代码就是对程序自己最好的注释。当你打算要添加注释时,问问自己,“我如何能改进编码以至于根本不需要添加注释?”改进你的代码,然后才是用注释使它更清楚。