📄 java 实现自动监听并更新配置文件内容.mht
字号:
#008000"><BR> * =E7=9B=91=E5=90=AC=E9=85=
=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E8=A2=AB=E6=9B=B4=E6=96=B0=
=E3=80=82<BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> todoTestFileStatus(){<BR> &nb=
sp; System.out.println(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">Getting file status</SPAN><SPAN =
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> System.out=
.println(</SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: #000000">.isFileUpdated(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">WEB-INF/platforms/test.properties</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">));<BR> }<BR> <BR>=
</SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E7=9B=91=E5=90=AC=E9=85=
=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E8=A2=AB=E6=9B=B4=E6=96=B0=
=EF=BC=8C=E8=87=AA=E5=8A=A8=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6=E4=B8=AD=E7=
=9A=84=E9=85=8D=E7=BD=AE=E9=A1=B9=E5=AD=98=E5=82=A8=E5=88=B0 applica=
tion =E5=8F=98=E9=87=8F=E4=B8=AD=E3=80=82<BR>  =
; </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> todo(){<BR>  =
;String filename </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">WEB-INF/platforms/test.properties</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">;<BR> </SPAN><SPA=
N=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN style=3D"COLOR: =
#000000">(</SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: =
#000000">.isFileUpdated(filename)){<BR> &nbs=
p; <BR> &=
nbsp; System.out.println(</SPAN><SPAN =
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">Getting properties</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">try</SPAN><SPAN=20
style=3D"COLOR: =
#000000">{<BR>  =
; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: #000000">.loadProperties(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">test</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">, filename);<BR> &=
nbsp; }</SPAN><SPAN=20
style=3D"COLOR: #0000ff">catch</SPAN><SPAN=20
style=3D"COLOR: =
#000000">(IOException ioe){<BR> &=
nbsp; System.err.pri=
ntln(ioe.getMessage());<BR> &nbs=
p; }<BR> =
}<BR> System.out.pri=
ntln(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">Test value is: </SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #000000">+</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: #000000">.getTestProperty(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: #000000">name</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">));<BR> }<BR> <BR>=
</SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> run() {<BR> =
todoTestRandom();<BR> &nbs=
p;todo();<BR> </SPAN><SPAN=
=20
style=3D"COLOR: #008000">//</SPAN><SPAN=20
style=3D"COLOR: #008000">todo();</SPAN><SPAN=20
style=3D"COLOR: #008000"><BR></SPAN><SPAN=20
style=3D"COLOR: =
#000000"> }<BR> <BR> &=
nbsp; </SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E5=88=A4=E6=96=AD=E7=89=
=A9=E7=90=86=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E5=B7=B2=E8=A2=AB=E6=9B=B4=
=E6=96=B0<BR> * </SPAN><SPAN=20
style=3D"COLOR: #808080">@param</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> filename =E7=89=A9=E7=90=86=E6=96=87=E4=BB=B6=E5=90=8D=
<BR> * </SPAN><SPAN=20
style=3D"COLOR: #808080">@return</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> =E6=98=AF true =E5=90=A6 false<BR> &nb=
sp; </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">private</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">boolean</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> isFileUpdated(String filename){<BR> =
File file </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">new</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> File(context.getRealPath(filename));<BR> =
</SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: =
#000000">(file.isFile()){<BR> &n=
bsp; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">long</SPAN><SPAN=20
style=3D"COLOR: #000000"> lastUpdateTime </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> file.lastModified();<BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000">(lastUpdateTime </SPAN><SPAN=20
style=3D"COLOR: #000000">></SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: =
#000000">.lastModified){<BR> &nb=
sp; System.out.println(</S=
PAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">The properties file was modified.</SPAN><SPA=
N=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">this</SPAN><SPAN=20
style=3D"COLOR: #000000">.lastModified </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> lastUpdateTime;<BR> &nb=
sp; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">true</SPAN><SPAN=20
style=3D"COLOR: =
#000000">;<BR>  =
; }</SPAN><SPAN=20
style=3D"COLOR: #0000ff">else</SPAN><SPAN=20
style=3D"COLOR: =
#000000">{<BR>  =
; System.out.println(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">The properties file was not modified.</=
SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: =
#000000">;<BR>  =
; }<BR> }</SPAN=
><SPAN=20
style=3D"COLOR: #0000ff">else</SPAN><SPAN=20
style=3D"COLOR: =
#000000">{<BR>  =
; System.out.println(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">The path does not point to a =
file.</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COLOR: =
#000000">);<BR> &nbs=
p; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">return</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">false</SPAN><SPAN=20
style=3D"COLOR: =
#000000">;<BR> }<BR> =
}<BR> <BR> &nbs=
p;</SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E8=8E=B7=E5=8F=96=E9=85=
=8D=E7=BD=AE=E6=96=87=E4=BB=B6<BR> * </=
SPAN><SPAN=20
style=3D"COLOR: #808080">@param</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> key<BR> * </SPAN><SPAN=20
style=3D"COLOR: #808080">@param</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> filename<BR> * </SPAN><S=
PAN=20
style=3D"COLOR: #808080">@return</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">void</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> loadProperties(String key, String filename)=
</SPAN><SPAN=20
style=3D"COLOR: #0000ff">throws</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> IOException{<BR> =
Properties prop </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: #000000"> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">new</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> Properties();<BR>  =
; InputStream stream </SPAN><SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> context.getResourceAsStream(filename);<BR> &nbs=
p; prop.load(stream);<BR> &=
nbsp; </SPAN><SPAN=20
style=3D"COLOR: #0000ff">if</SPAN><SPAN=20
style=3D"COLOR: #000000">(stream</SPAN><SPAN=20
style=3D"COLOR: #000000">!=3D</SPAN><SPAN=20
style=3D"COLOR: #0000ff">null</SPAN><SPAN=20
style=3D"COLOR: =
#000000">){<BR> &nbs=
p; stream.close();<BR> &nbs=
p; }<BR> context.setA=
ttribute(key, prop);<BR> }<BR> &nb=
sp; <BR> </SPAN><SPAN=20
style=3D"COLOR: #008000">/**</SPAN><SPAN=20
style=3D"COLOR: =
#008000"><BR> * =E4=BB=8E applicat=
ion =E5=8F=96=E9=85=8D=E7=BD=AE=E9=A1=B9=E7=9A=84=E5=80=BC<BR> =
* </SPAN><SPAN=20
style=3D"COLOR: #808080">@param</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> key =E9=85=8D=E7=BD=AE=E9=A1=B9=E7=9A=84=E9=94=AE=E5=90=
=8D<BR> * </SPAN><SPAN=20
style=3D"COLOR: #808080">@return</SPAN><SPAN=20
style=3D"COLOR: =
#008000"> =E9=85=8D=E7=BD=AE=E9=A1=B9=E7=9A=84=E5=80=BC<BR> &nb=
sp; </SPAN><SPAN=20
style=3D"COLOR: #008000">*/</SPAN><SPAN=20
style=3D"COLOR: #000000"><BR> </SPAN><SPAN=20
style=3D"COLOR: #0000ff">public</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> String getTestProperty(String key){<BR> &nb=
sp; Properties prop </SPAN><=
SPAN=20
style=3D"COLOR: #000000">=3D</SPAN><SPAN=20
style=3D"COLOR: =
#000000"> (Properties)context.getAttribute(</SPAN><SPAN=20
style=3D"COLOR: #000000">"</SPAN><SPAN=20
style=3D"COL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -