[英]edit a line in text file
so i have a text file that consist: 所以我有一个文本文件,包括:
not voting/1/harold/18
not voting/2/isabel/24
this describes like not voting/number for vote/name/age
. 这描述了
not voting/number for vote/name/age
。 my goal is to edit not voting
to voted
but still keeping the other info ( number for vote/name/age
). 我的目标是编辑
not voting
不voted
但仍保留其他信息( number for vote/name/age
)。 the user will input the number for vote then if it exist, the not voting
will automatically change to voted
用户将输入投票号码,如果存在,则
not voting
将自动更改为voted
here is my code: 这是我的代码:
File original = new File("C:\\voters.txt");
File temporary = new File("C:\\tempvoters.txt");
BufferedReader infile = new BufferedReader(new FileReader(original));
PrintWriter outfile = new PrintWriter(new PrintWriter(temporary));
numberforvote=JOptionPane.showInputDialog("Enter voters number: ");
String line=null;
while((line=infile.readLine())!=null){
String [] info=line.split("/");
if(info[1].matches(numberforvote)){
all="VOTED"+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
outfile.println(all);
outfile.flush();
}
}
infile.close();
outfile.close();
original.delete();
temporary.renameTo(original);
this works but the problem with my code is the second line ( not voting/2/isabel/24
) will disappear/deleted. 这工作但我的代码的问题是第二行(
not voting/2/isabel/24
)将消失/删除。 i want everything to be the same except for the not voting
in the given/entered number for vote. 我希望一切都是一样的,除非
not voting
给定/输入的数字没有投票。
if(info[1].matches(numberforvote)){
all="VOTED"+"/"+info[1]+"/"...;
outfile.println(all);
outfile.flush();
} else {
outfile.println( line );
}
Copy to output if there's no match. 如果没有匹配则复制到输出。
I should add that using a regex for a single string compare should be reduced to the simpler info[1].equals(numberforvote)
. 我应该补充说,使用正则表达式进行单个字符串比较应该简化为更简单的
info[1].equals(numberforvote)
。 But calling numberforvote = numberforvote.trim();
但是调用
numberforvote = numberforvote.trim();
could be useful. 可能有用。
Your output file gets entirely overwritten , so you have to write all lines, even those that you didn't intend to modify : 您的输出文件被完全覆盖,因此您必须编写所有行,即使是那些您不打算修改的行:
if(info[1].matches(numberforvote)){
all="VOTED"+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
outfile.println(all);
}
else{
outfile.println(line); // this will write the "unchanged" lines
}
outfile.flush();
Move it outside the if
and only change the part you need to change. 将它移到
if
之外,只更改你需要更改的部分。 That way you can change whatever you want then rebuild the line. 这样你可以改变你想要的任何东西然后重建线。
if(info[1].matches(numberforvote)){
into[0] = VOTED;
}
all=info[0]+"/"+info[1]+"/"+info[2]+"/"+info[3]+"/"+info[4]+"/"+info[5]+"/"+info[6]+"/"+info[7]+"/"+info[8]+"/"+info[9]+"/"+info[10]+"/"+info[11]+"/"+info[12];
outfile.println(all);
outfile.flush();
or clean up that ugly line 或清理那条丑陋的线条
StringBuilder sb = new StringBuilder();
for (String element : info){
sb.append(element);
}
outfile.println(sb.toString());
Other Answers 其他答案
You could just output the unchanged line like others have suggested 您可以像其他人建议的那样输出未更改的行
outfile.println(line);
but it's not as flexable if you want to make other changes later. 但如果您想稍后进行其他更改,则不具备灵活性。
You should simplify the split and writing to: 您应该简化拆分并写入:
String [] info=line.split("/",2);
if ( info.length == 2 ) {
...
outfile.println("VOTED/"+info[1]);
} else {
// input error
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.