编辑文本文件中的一行
edit a line in text file
所以我有一个包含以下内容的文本文件:
not voting/1/harold/18
not voting/2/isabel/24
这描述得像 not voting/number for vote/name/age
。我的目标是将 not voting
编辑为 voted
,但仍保留其他信息 (number for vote/name/age
)。用户将输入投票号码,如果存在,not voting
将自动更改为 voted
这是我的代码:
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);
这行得通,但我的代码的问题是第二行 (not voting/2/isabel/24
) 将 disappear/deleted。我希望除了 given/entered 投票号码中的 not voting
之外,其他所有内容都相同。
if(info[1].matches(numberforvote)){
all="VOTED"+"/"+info[1]+"/"...;
outfile.println(all);
outfile.flush();
} else {
outfile.println( line );
}
如果不匹配则复制到输出。
我应该补充一点,使用正则表达式进行单个字符串比较应该减少到更简单的 info[1].equals(numberforvote)
。但是调用 numberforvote = numberforvote.trim();
可能会有用。
你的输出文件被完全覆盖,所以你必须写下所有行,即使是那些你不打算修改的行:
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();
将其移到if
之外,只更改您需要更改的部分。这样你就可以改变任何你想要的然后重建线。
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();
或者清理那条丑陋的线
StringBuilder sb = new StringBuilder();
for (String element : info){
sb.append(element);
}
outfile.println(sb.toString());
其他答案
您可以像其他人建议的那样输出未更改的行
outfile.println(line);
但如果您想稍后进行其他更改,它就不那么灵活了。
你应该简化拆分并写信给:
String [] info=line.split("/",2);
if ( info.length == 2 ) {
...
outfile.println("VOTED/"+info[1]);
} else {
// input error
}
所以我有一个包含以下内容的文本文件:
not voting/1/harold/18
not voting/2/isabel/24
这描述得像 not voting/number for vote/name/age
。我的目标是将 not voting
编辑为 voted
,但仍保留其他信息 (number for vote/name/age
)。用户将输入投票号码,如果存在,not voting
将自动更改为 voted
这是我的代码:
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);
这行得通,但我的代码的问题是第二行 (not voting/2/isabel/24
) 将 disappear/deleted。我希望除了 given/entered 投票号码中的 not voting
之外,其他所有内容都相同。
if(info[1].matches(numberforvote)){
all="VOTED"+"/"+info[1]+"/"...;
outfile.println(all);
outfile.flush();
} else {
outfile.println( line );
}
如果不匹配则复制到输出。
我应该补充一点,使用正则表达式进行单个字符串比较应该减少到更简单的 info[1].equals(numberforvote)
。但是调用 numberforvote = numberforvote.trim();
可能会有用。
你的输出文件被完全覆盖,所以你必须写下所有行,即使是那些你不打算修改的行:
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();
将其移到if
之外,只更改您需要更改的部分。这样你就可以改变任何你想要的然后重建线。
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();
或者清理那条丑陋的线
StringBuilder sb = new StringBuilder();
for (String element : info){
sb.append(element);
}
outfile.println(sb.toString());
其他答案
您可以像其他人建议的那样输出未更改的行
outfile.println(line);
但如果您想稍后进行其他更改,它就不那么灵活了。
你应该简化拆分并写信给:
String [] info=line.split("/",2);
if ( info.length == 2 ) {
...
outfile.println("VOTED/"+info[1]);
} else {
// input error
}