我正在處理屬性文件。關於它,我有一個關鍵是「user_email」和值,我把它設置爲[email protected]。我想獲得下一個遞增值9(當然是10),但它返回一個ASCII碼
現在,在我的代碼中,我希望電子郵件的值能夠在我的程序運行時進行迭代,因此[email protected]將會是[email protected]等等,並且從那裏讀取屬性中的電子郵件值文件並在我的程序中調用它。
public String getEmailFromProperty(){
String new_email = "";
try{
new ConfigReader();
FileOutputStream fos = new FileOutputStream(property_file);
StringBuilder str = new StringBuilder(property.getProperty("user_email"));
char charWithNumber = str.charAt(9); // get the number character of the email (starts with 0)
int toInteger = (int) charWithNumber; // convert that character number to an Integer value
int numericValue = Character.getNumericValue(toInteger+1); // the number character in email is converted to int then incremented by 1 on each run
int toAscii = numericValue + 48; // the number character (which is now an Int) is added by 48
char toChar = (char) toAscii; // it is converted back to a character in order for it to be passed as a parameter to setCharAt() method
str.setCharAt(9, toChar); // attached the newly incremented number character to the email @ 9th index
new_email = str.toString(); // converted the StringBuilder variable str to an ordinary String in order to call toString() method
property.setProperty("user_email", new_email); // now, I wrote the new email to the property file using the "user_email" key
property.store(fos, null);
fos.close();
}
catch(Exception e){
System.out.println("Error is " + e.getMessage());
}
return new_email;
}
我知道這對你來說有點麻煩。但是,當電子郵件號字符達到值9,然後它增加,我預計它是10.但是,它返回'/'字符。我想運行後[email protected]不toomeuser/@gmail.com
如果您要替換單個字符,您如何期待'9'被替換爲「10」? – Eran