我正在处理一个属性文件。在它上面,我有一个键“user_email”和值,我将它设置为toomeuser0@gmail.com。
现在,在我的代码中,我希望在我的程序运行时迭代电子邮件值,因此 toomeuser0@gmail.com 将是 toomeuser1@gmail.com 等等,然后从那里读取属性文件中的电子邮件值并调用它在我的程序中。
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。但是,它返回 '/' 字符。跑步后我想要的是toomeuser10@gmail.com 而不是toomeuser/@gmail.com