java中按名称更新txt文件中两列的记录
我有员工的txt文件 它包含名称,日期,帖子,薪水:
Asher 2022/04/05 HR-Manager 24000
我更改了我的代码,这效果很好(也许对某人有帮助):
Scanner sc = new Scanner(System.in);
System.out.println("•\tWrite name of employee to change salary and post");
String name = sc.nextLine();
System.out.println("•\tNew post:");
String post = sc.nextLine();
System.out.println("•\tNew salary:");
String salary = sc.nextLine();
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(oldFileName));
bw = new BufferedWriter(new FileWriter(tmpFileName));
String line;
while ((line = br.readLine()) != null) {
String[] arr =line.split(" ");
if (line.contains(name))
line = line.replace(arr[2], post);
bw.write(line+"\n");
}
I have txt file with employees
It contains name, date, post, salary:
Asher 2022/04/05 HR-Manager 24000
I've changed my code, this one works fine (maybe would be helpfull for someone):
Scanner sc = new Scanner(System.in);
System.out.println("•\tWrite name of employee to change salary and post");
String name = sc.nextLine();
System.out.println("•\tNew post:");
String post = sc.nextLine();
System.out.println("•\tNew salary:");
String salary = sc.nextLine();
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(oldFileName));
bw = new BufferedWriter(new FileWriter(tmpFileName));
String line;
while ((line = br.readLine()) != null) {
String[] arr =line.split(" ");
if (line.contains(name))
line = line.replace(arr[2], post);
bw.write(line+"\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论