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) {
if (line.contains("Smokey")){
line = line.replace("Smokey;","AAAAAA;");
bw.write(line+"\n");
} else {
bw.write(line+"\n");
}
}
}
catch (Exception e) {
return;
} finally {
try {
if(br != null){
br.close();
messagejLabel.setText("Error");
}
} catch (IOException e) {
}
}
// Once everything is complete, delete old file..
File oldFile = new File(oldFileName);
oldFile.delete();
// And rename tmp file's name to old file name
File newFile = new File(tmpFileName);
newFile.renameTo(oldFile);
运行上面的代码时,我最终得到一个空文件“tmpfiles.txt”并且文件“files.txt 正在被删除。谁能帮忙?我不想使用字符串来读取文件。我宁愿按照他的方式去做。