<div class="htmledit_views">html
<ol start="1" class="dp-j" style="border:none;color:rgb(92,92,92);font-family:Consolas, 'Courier New', Courier, mono, serif;line-height:26px;"><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">* 實現對Java配置文件Properties的讀取、寫入與更新操做 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">*/</span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">package</span><span style="border:none;background-color:inherit;"> test; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.BufferedInputStream; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.FileInputStream; </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.FileNotFoundException; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.FileOutputStream; </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.IOException; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.InputStream; </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.io.OutputStream; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">import</span><span style="border:none;background-color:inherit;"> java.util.Properties; </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">* @author </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">* @version </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">*/</span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">public</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">class</span><span style="border:none;background-color:inherit;"> SetSystemProperty { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">//屬性文件的路徑 </span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> String profilepath=</span><span class="string" style="border:none;color:#FF0000;background-color:inherit;">"mail.properties"</span><span style="border:none;background-color:inherit;">; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 採用靜態方法 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> */</span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">private</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> Properties props = </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> Properties(); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">try</span><span style="border:none;background-color:inherit;"> { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> props.load(<span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> FileInputStream(profilepath)); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">catch</span><span style="border:none;background-color:inherit;"> (FileNotFoundException e) { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> e.printStackTrace(); </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> System.exit(-<span class="number" style="border:none;color:rgb(192,0,0);background-color:inherit;">1</span><span style="border:none;background-color:inherit;">); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> } <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">catch</span><span style="border:none;background-color:inherit;"> (IOException e) { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> System.exit(-<span class="number" style="border:none;color:rgb(192,0,0);background-color:inherit;">1</span><span style="border:none;background-color:inherit;">); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 讀取屬性文件中相應鍵的值 </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param key </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 主鍵 </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @return String </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> */</span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">public</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> String getKeyValue(String key) { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">return</span><span style="border:none;background-color:inherit;"> props.getProperty(key); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 根據主鍵key讀取主鍵的值value </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param filePath 屬性文件路徑 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param key 鍵名 </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> */</span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">public</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> String readValue(String filePath, String key) { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> Properties props = <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> Properties(); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">try</span><span style="border:none;background-color:inherit;"> { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> InputStream in = <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> BufferedInputStream(</span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> FileInputStream( </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> filePath)); </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> props.load(in); </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> String value = props.getProperty(key); </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> System.out.println(key +<span class="string" style="border:none;color:#FF0000;background-color:inherit;">"鍵的值是:"</span><span style="border:none;background-color:inherit;">+ value); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">return</span><span style="border:none;background-color:inherit;"> value; </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">catch</span><span style="border:none;background-color:inherit;"> (Exception e) { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> e.printStackTrace(); </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">return</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">null</span><span style="border:none;background-color:inherit;">; </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 更新(或插入)一對properties信息(主鍵及其鍵值) </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 若是該主鍵已經存在,更新該主鍵的值; </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 若是該主鍵不存在,則插件一對鍵值。 </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param keyname 鍵名 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param keyvalue 鍵值 </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> */</span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">public</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">void</span><span style="border:none;background-color:inherit;"> writeProperties(String keyname,String keyvalue) { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">try</span><span style="border:none;background-color:inherit;"> { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 調用 Hashtable 的方法 put,使用 getProperty 方法提供並行性。 </span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 強制要求爲屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。 </span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> OutputStream fos = <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> FileOutputStream(profilepath); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> props.setProperty(keyname, keyvalue); </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 以適合使用 load 方法加載到 Properties 表中的格式, </span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流 </span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> props.store(fos, <span class="string" style="border:none;color:#FF0000;background-color:inherit;">"Update '"</span><span style="border:none;background-color:inherit;"> + keyname + </span><span class="string" style="border:none;color:#FF0000;background-color:inherit;">"' value"</span><span style="border:none;background-color:inherit;">); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">catch</span><span style="border:none;background-color:inherit;"> (IOException e) { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> System.err.println(<span class="string" style="border:none;color:#FF0000;background-color:inherit;">"屬性文件更新錯誤"</span><span style="border:none;background-color:inherit;">); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">/** </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 更新properties文件的鍵值對 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 若是該主鍵已經存在,更新該主鍵的值; </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * 若是該主鍵不存在,則插件一對鍵值。 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param keyname 鍵名 </span> </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> * @param keyvalue 鍵值 </span> </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"><span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;"> */</span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">public</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">void</span><span style="border:none;background-color:inherit;"> updateProperties(String keyname,String keyvalue) { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">try</span><span style="border:none;background-color:inherit;"> { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> props.load(<span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> FileInputStream(profilepath)); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 調用 Hashtable 的方法 put,使用 getProperty 方法提供並行性。 </span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 強制要求爲屬性的鍵和值使用字符串。返回值是 Hashtable 調用 put 的結果。 </span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> OutputStream fos = <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">new</span><span style="border:none;background-color:inherit;"> FileOutputStream(profilepath); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> props.setProperty(keyname, keyvalue); </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 以適合使用 load 方法加載到 Properties 表中的格式, </span><span style="border:none;background-color:inherit;"> </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">// 將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流 </span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> props.store(fos, <span class="string" style="border:none;color:#FF0000;background-color:inherit;">"Update '"</span><span style="border:none;background-color:inherit;"> + keyname + </span><span class="string" style="border:none;color:#FF0000;background-color:inherit;">"' value"</span><span style="border:none;background-color:inherit;">); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">catch</span><span style="border:none;background-color:inherit;"> (IOException e) { </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> System.err.println(<span class="string" style="border:none;color:#FF0000;background-color:inherit;">"屬性文件更新錯誤"</span><span style="border:none;background-color:inherit;">); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="comment" style="border:none;color:rgb(0,130,0);background-color:inherit;">//測試代碼 </span><span style="border:none;background-color:inherit;"> </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> <span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">public</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">static</span><span style="border:none;background-color:inherit;"> </span><span class="keyword" style="border:none;color:#0000FF;font-weight:bold;background-color:inherit;">void</span><span style="border:none;background-color:inherit;"> main(String[] args) { </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> readValue(<span class="string" style="border:none;color:#FF0000;background-color:inherit;">"mail.properties"</span><span style="border:none;background-color:inherit;">, </span><span class="string" style="border:none;color:#FF0000;background-color:inherit;">"MAIL_SERVER_PASSWORD"</span><span style="border:none;background-color:inherit;">); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> writeProperties(<span class="string" style="border:none;color:#FF0000;background-color:inherit;">"MAIL_SERVER_INCOMING"</span><span style="border:none;background-color:inherit;">, </span><span class="string" style="border:none;color:#FF0000;background-color:inherit;">"327@qq.com"</span><span style="border:none;background-color:inherit;">); </span></span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;"> System.out.println(<span class="string" style="border:none;color:#FF0000;background-color:inherit;">"操做完成"</span><span style="border:none;background-color:inherit;">); </span></span></li><li class="alt" style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:inherit;line-height:18px;"> <span style="border:none;color:#000000;background-color:inherit;"> } </span></li><li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(153,153,153);list-style:outside;color:rgb(85,85,85);line-height:18px;background-color:rgb(248,248,248);"> <span style="border:none;color:#000000;background-color:inherit;">} </span></li></ol> </div>java