FileInputStream input1 = new FileInputStream ("/jdbc.properties")//Read code
safeproperties SAFEPROP1 = new Safeproperties ();
Safeprop1.load (INPUT1);
String safe= safeprop1.getproperty ("url");
Input1.close ();
FileInputStream input = new FileInputStream ("sso.properties")//Modify code
safeproperties Safeprop = new Safeproperties ( );
Safeprop.load (input);
Input.close ();
Safeprop.setproperty ("Sso.server.urlprefix", "Test");
FileOutputStream output = new FileOutputStream ("Sso.properties");
Safeprop.store (output, null);
Output.close ();
Overriding Properties Class
Import java.io.*;
Import java.util.ArrayList;
Import Java.util.Iterator;
Import java.util.List;
Import java.util.Properties;
/** * Created by 31767 on 2017/8/8. * * public class Safeproperties extends Properties {private static final long Serialversionuid = 5011694856722313621L
;
private static final String keyvalueseparators = "=: \t\r\n\f";
private static final String strictkeyvalueseparators = "=:";
private static final String Specialsavechars = "=: \t\r\n\f#!";
private static final String Whitespacechars = "\t\r\n\f";
Private Propertiescontext context = new Propertiescontext ();
Public Propertiescontext GetContext () {return context;
synchronized void load (InputStream instream) throws IOException {BufferedReader in;
in = new BufferedReader (new InputStreamReader (instream, "8859_1"));
while (true) {String line = In.readline ();
String intactline = line; If (line = null) return;
if (line.length () > 0) {int len = line.length ();
int Keystart; for (Keystart = 0; Keystart < len; keystart++) if (Whitespacechars.indexof (Line.charat (keystart)) =
=-1) break;
if (Keystart = = len) continue;
Char Firstchar = Line.charat (Keystart); if ((Firstchar!= ' # ') && (Firstchar!= '! '))
{while (continueline) {String nextline = In.readline ();
Intactline = intactline + "\ n" + nextline;
if (nextline = = null) nextline = "";
String loppedline = line.substring (0, len-1);
int startIndex;
for (startIndex = 0; StartIndex < Nextline.length (); startindex++) if (Whitespacechars.indexof (Nextline.charat (startIndex)) = = 1)
Break
nextline = nextline.substring (StartIndex, Nextline.length ());
line = new String (loppedline + nextline);
Len = Line.length ();
int separatorindex; for (Separatorindex = Keystart; Separatorindex < Len; separatorindex++) {char Currentchar = Li
Ne.charat (Separatorindex);
if (Currentchar = = ' \ ') separatorindex++;
else if (Keyvalueseparators.indexof (Currentchar)!=-1) break;
int valueindex; for (ValueIndex = Separatorindex; valueindex < Len; valueindex++) if (Whitespacechars.indexof (L) Ine.charat (valueindex) = = 1) break;
if (ValueIndex < len) if (Strictkeyvalueseparators.indexof (Line.charat (valueindex))!=-1)
valueindex++;
while (ValueIndex < Len) {if (Whitespacechars.indexof (Line.charat (valueindex)) = = 1)
Break
valueindex++;
String key = Line.substring (Keystart, Separatorindex); String value = (Separatorindex < len)?
Line.substring (ValueIndex, Len): "";
Key = Loadconvert (key);
Value = Loadconvert (value);
Put (key, value, Intactline);
else {context.addcommentline (intactline);
} else {context.addcommentline (intactline); }} Private String loAdconvert (String thestring) {char achar;
int len = Thestring.length ();
StringBuffer outbuffer = new StringBuffer (len); for (int x = 0; x < len;)
{Achar = Thestring.charat (x + +);
if (Achar = = ' \ ") {Achar = Thestring.charat (x + +);
if (Achar = = ' U ') {//Read the xxxx int value = 0;
for (int i = 0; i < 4; i++) {Achar = Thestring.charat (x + +); Switch (achar) {case ' 0 ': Case ' 1 ': C
ASE ' 2 ': Case ' 3 ': Case ' 4 ': Case ' 5 ':
Case ' 6 ': Case ' 7 ': Case ' 8 ': Case ' 9 ': value = (Value <<4) + Achar-' 0 ';
Break Case ' A ': Case ' B ': Case ' C ': Case ' d ' : Case ' E ': Case ' f ': value = (val
UE << 4) + + Achar-' a ';
Break Case ' A ': Case ' B ': Case ' C ': Case ' D ' : Case ' E ': Case ' F ': value = (val
UE << 4) + + Achar-' A ';
Break
Default:throw New IllegalArgumentException ("Malformed \\UXXXX encoding.");
} outbuffer.append ((char) value); else {if (Achar = = ' t ') outbuffer.append (' \ t ');/* ibm@7211 * else if (Achar = = ' R ') outbuffer.append (' \ R '); /* ibm@7211 */else if (Achar = = ' n ') {/* ibm@8897 does not convert a \ n to a Line.separ Ator * Because on some platforms line.separator is a String * of "\ r \ n". When a Properties class is saved as a file * (Store ()) and then restored (load ()) the restored * input must
Being the same as the output (so, * properties.equals () works). * * */outbuffer.append (' \ n '); /* ibm@8897 ibm@7211/else if (Achar = = ' F ') outbuffer.append (' \f '); * IBM@7211 */Else * ibm@7211 * * Outbuffer.append (ACHAR);
/* ibm@7211/} else Outbuffer.append (Achar); return outbuffer.tostring (); synchronized void Store (OutputStream out, String header) throws IOException {BufferedWriter Awriter
;
Awriter = new BufferedWriter (new OutputStreamWriter (out, "8859_1"));
if (header!= null) writeln (Awriter, "#" + header);
List Entrys = Context.getcommentorentrys (); for (Iterator iter = Entrys.iterator (); Iter.hasnext ();)
{Object obj = Iter.next ();
if (obj.tostring ()!= null) {Writeln (Awriter, obj.tostring ());
} awriter.flush ();
private static void Writeln (BufferedWriter bw, String s) throws IOException {Bw.write (s);
Bw.newline ();
Private Boolean continueline (String line) {int slashcount = 0;
int index = Line.length ()-1;
while (index >= 0) && (line.charat (index--) = = ' \ ") slashcount++; Return (slashcount% 2= = 1); }/* Converts unicodes to encoded \uxxxx and writes out any of the * characters in specialsavechars with A preceding slash */private string Saveconvert (String TheString, Boolean escapespace) {int len = Thestr
Ing.length ();
StringBuffer outbuffer = new StringBuffer (len * 2);
for (int x = 0; x < len X + +) {char Achar = Thestring.charat (x); Switch (achar) {case ': if (x = = 0 | | escapespace) Outbuffe
R.append (' \ \ ');
Outbuffer.append (");
Break
Case ' \ ': outbuffer.append (' \ \ ');
Outbuffer.append (' \ \ ');
Break
Case ' t ': outbuffer.append (' \ \ ');
Outbuffer.append (' t ');
Break
Case ' \ n ': outbuffer.append (' \ \ '); Outbuffer.append (' n ');
Break
Case ' \ R ': outbuffer.append (' \ \ ');
Outbuffer.append (' R ');
Break
Case ' \f ': outbuffer.append (' \ \ ');
Outbuffer.append (' f ');
Break Default:if (Achar < 0x0020) | | (Achar > 0x007e))
{outbuffer.append (' \ \ ');
Outbuffer.append (' u ');
Outbuffer.append (Tohex (Achar >>) & 0xF));
Outbuffer.append (Tohex (Achar >> 8) & 0xF));
Outbuffer.append (Tohex (Achar >> 4) & 0xF));
Outbuffer.append (Tohex (Achar & 0xF)); } else {if (Specialsavechars.indexof (Achar)!=-1) outbuffer.append ('
\\'); Outbuffer.append (Achar);
}} return outbuffer.tostring ();
/** * Convert a nibble to a hex character * * @param nibble * The nibble to convert.
* * private static char tohex (int nibble) {return hexdigit[(nibble & 0xF)]; }/** A Table of hex digits/private static final char[] Hexdigit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', '
8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
Public synchronized object (object key, Object value) {context.putorupdate (key.tostring (), value.tostring ());
Return Super.put (key, value); The public synchronized object is put (object key, object value, String line) {context.putorupdate (key.tostring ()),
Value.tostring (), line);
Return Super.put (key, value);
Public Synchronized object Remove (object key) {Context.remove (key.tostring ()); Return SUPER.REmove (key);
Class Propertiescontext {private List Commentorentrys = new ArrayList ();
Public List Getcommentorentrys () {return commentorentrys;
public void Addcommentline (String line) {Commentorentrys.add (line);
public void Putorupdate (Propertyentry pe) {remove (Pe.getkey ());
Commentorentrys.add (PE); public void Putorupdate (string key, String value, String line) {Propertyentry PE = new propertyent
Ry (key, value, line);
Remove (key);
Commentorentrys.add (PE); public void Putorupdate (string key, String value) {propertyentry PE = new Propertyentry (key, value
);
int index = remove (key);
Commentorentrys.add (INDEX,PE);
public int Remove (String key) {for (int index = 0; index < commentorentrys.size (); index++) { Object obj = Commentorentrys.get (index); if (obj instanceof propertyentry) {if (obj!= null) {if (Key.equals (Proper
tyentry) obj). Getkey ())) {commentorentrys.remove (obj);
return index;
}}} return Commentorentrys.size ();
Class Propertyentry {private String key;
private String value;
Private String Line;
Public String getline () {return line;
public void Setline (String line) {this.line = line;
Public Propertyentry (String key, String value) {This.key = key;
This.value = value;
}/** * @param key * @param value * @param line * * Public Propertyentry (String key, String value, String line) {This (key, value);
This.line = line;
Public String Getkey () {return key;
public void Setkey (String key) {this.key = key;
Public String GetValue () {return value;
public void SetValue (String value) {this.value = value;
Public String toString () {if (line!= null) {return line;
} if (key!= null && value!= null) {String k = Saveconvert (key, true);
String v = Saveconvert (value, false);
return k + "=" + V;
return null; }} public void AddComment (String comment) {if (comment!= null) {Context.addcommentline ("#" + comment); }
}
}