此代码更改修改数据,它只保留修改的行而不是其余行。
bw = new BufferedWriter(new FileWriter(file, true));
replacedLine = CurrentLine.replace(PASSWORD,password);
bw.write(replacedLine);
RESIDENTS_OLDDETAILS = new File(file);
RESIDENTS_OLDDETAILS.delete();
RESIDENTS_NEWDETAILS = new File(newfile);
RESIDENTS_NEWDETAILS.renameTo(file);
bw.close();
我想更改旧的行细节,并保持其余的行相同。
示例:
abc,1234,test
ddd,2345,test1
到
abc,9999,test
ddd,2345,test1
答案 0 :(得分:0)
您可以将所有更新的行放在新变量中,最后将这些新行写入新文件中。
StringBuilder updatedContents = new StringBuilder();
while(currentLine != null) {
updatedContents.append(currentLine.replace(PASSWORD, password));
updatedContents.append("\n");
currentLine = reader.readLine();
}
BufferedWriter writer = new BufferedWriter(new FileWriter("filename"));
writer.write(updatedContents.toString());
writer.close();
我认为这会对你有帮助..