Java 用文件中的分隔符替换字段 (String/Int)
Java Replacing a Field (String/Int) with delimiter from file
我在下面得到这段代码来替换可以是 String 或 Int 的字段,但它没有按预期工作。我想做的是在文件 player.dat 中,字段是 "Username|Password|No. of Chips" 我想根据用户名替换第三个字段,我使用字符串替换将旧字符串更改为新字符串但是它不工作它只是在旧值旁边添加数字,我文件中的所有数据都与原始数据逐行混淆。在这方面需要一些帮助
当前输出:
Username1|HashedPassword|200200Username2|HashedPassword2|300200Username3|HashedPassword3|300200
预期输出
Username1|HashedPassword|200
Username2|HashedPassword|300
Username3|HashedPassword|200
代码如下所示。
public class testcode {
public static void main(String[] args) {
new testcode().replace();
}
public void replace() {
String oldFileName = "players.dat";
String tmpFileName = "tempmod.dat";
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<String> player = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(oldFileName));
bw = new BufferedWriter(new FileWriter(tmpFileName));
String line;
Scanner read = new Scanner(System.in);
System.out.println("Please Enter Username");
String UserN = read.nextLine();
System.out.println("Please Enter Chips to Add");
String UserCadd = read.nextLine();
while ((line = br.readLine()) != null) {
String[] details = line.split("\|");
String Username = details[0];
String Password = details[1];
String Chips = details[2];
int totalChips = Integer.parseInt(UserCadd) + Integer.parseInt(Chips);
if (Username.equals(UserN))
line = Username + "|" + Password + "|" +totalChips;
bw.write("\r\n"+line);
}
} catch (Exception e) {
return;
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
//
}
try {
if(bw != null)
bw.close();
} catch (IOException e) {
//
}
}
}
}
您可以手动创建它:
Integer totalChips = (Integer.parseInt(UserCadd) + Integer.parseInt(Chips));
line = Username+"|"+Password+"|"+totalChips;
String totalChips = UserCadd + Chips;
这是一个字符串连接,而不是数字加法,所以你最终得到 200200
。
我认为您想在替换旧值之前添加实际数字。看看Integer.parseInt()
我在下面得到这段代码来替换可以是 String 或 Int 的字段,但它没有按预期工作。我想做的是在文件 player.dat 中,字段是 "Username|Password|No. of Chips" 我想根据用户名替换第三个字段,我使用字符串替换将旧字符串更改为新字符串但是它不工作它只是在旧值旁边添加数字,我文件中的所有数据都与原始数据逐行混淆。在这方面需要一些帮助
当前输出:
Username1|HashedPassword|200200Username2|HashedPassword2|300200Username3|HashedPassword3|300200
预期输出
Username1|HashedPassword|200
Username2|HashedPassword|300
Username3|HashedPassword|200
代码如下所示。
public class testcode {
public static void main(String[] args) {
new testcode().replace();
}
public void replace() {
String oldFileName = "players.dat";
String tmpFileName = "tempmod.dat";
BufferedReader br = null;
BufferedWriter bw = null;
ArrayList<String> player = new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(oldFileName));
bw = new BufferedWriter(new FileWriter(tmpFileName));
String line;
Scanner read = new Scanner(System.in);
System.out.println("Please Enter Username");
String UserN = read.nextLine();
System.out.println("Please Enter Chips to Add");
String UserCadd = read.nextLine();
while ((line = br.readLine()) != null) {
String[] details = line.split("\|");
String Username = details[0];
String Password = details[1];
String Chips = details[2];
int totalChips = Integer.parseInt(UserCadd) + Integer.parseInt(Chips);
if (Username.equals(UserN))
line = Username + "|" + Password + "|" +totalChips;
bw.write("\r\n"+line);
}
} catch (Exception e) {
return;
} finally {
try {
if(br != null)
br.close();
} catch (IOException e) {
//
}
try {
if(bw != null)
bw.close();
} catch (IOException e) {
//
}
}
}
}
您可以手动创建它:
Integer totalChips = (Integer.parseInt(UserCadd) + Integer.parseInt(Chips));
line = Username+"|"+Password+"|"+totalChips;
String totalChips = UserCadd + Chips;
这是一个字符串连接,而不是数字加法,所以你最终得到 200200
。
我认为您想在替换旧值之前添加实际数字。看看Integer.parseInt()