[英]How to replace a word in an exisiting text file in java
I want to replace a word to another word in an existing text file. 我想将一个单词替换为现有文本文件中的另一个单词。 I think my code is correct but whenever I run the program all the text gets deleted in the text file. 我认为我的代码是正确的,但是每当我运行程序时,所有文本都会在文本文件中删除。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String ar[]) throws IOException{
String getText, putText;
File f = new File("Test.txt");
FileReader fr ;
BufferedReader br ;
BufferedWriter bw;
if(f.exists()){
try{
fr = new FileReader(f);
br = new BufferedReader(fr);
bw = new BufferedWriter(new FileWriter(f));
while((getText = br.readLine()) != null){
System.out.println(getText);
if(getText != null){
putText = getText.replaceFirst("Dog", "Cat");
bw.write(putText);
}
}
bw.close();
br.close();
}
catch(IOException e){
e.printStackTrace();
}
}else{
System.out.println("No File exists!");
}
}
}
You simply should not read and write from/to the same file at the same point in time. 您根本不应该在同一时间在同一文件上读写。
Instead: read from file A ; 相反:从文件A读取; and write to some file A.tmp . 并写入一些文件A.tmp 。
And if you absolutely want to, you can still delete A , and rename A.tmp to A in the end. 而且,如果绝对需要,您仍然可以删除A ,最后将A.tmp重命名为A。
Finally, just for the record: if your goal is really just that; 最后,记录一下:如果您的目标确实如此; to replace some strings in some text file, the more reasonable answer would be to look into tools such sed (or some alternatives that exist for Windows OSes). 要替换某些文本文件中的某些字符串,更合理的答案是研究诸如sed之类的工具(或Windows操作系统中存在的一些替代方法)。
To replace only the first occurance of some string, you have to add some marker that tells you if you replaced already, like: 要仅替换某些字符串的第一次出现,必须添加一些标记来告诉您是否已替换,例如:
boolean replacingRequired = true;
while ( ... reading ...)
if (replacingRequired) {
putText = getText.replaceFirst("Dog", "Cat")
if (! putText.equals(getText)) {
// when they are different, you replaced something!
replacingRequired = false;
}
...
Final finally: as you are now asking about replacing the last occurance ... well, that is a completely different and harder problem. 最终的最终结果:正如您现在正在询问要替换上次出现的问题一样 ……那是一个完全不同且困难的问题。 You see, your current approach is to read that input file line by line, update that line, and write that to the new file. 您会看到,当前的方法是逐行读取该输入文件,更新该行并将其写入新文件。 But: how could code that goes line-by-line know that this line ... is the last one that carries the pattern that you wish to replace?! 但是:逐行执行的代码如何知道这一行...是最后一个承载您希望替换的模式的代码? Short answer: it can't. 简短的回答:不能。
The simply solution here; 这里的简单解决方案; which works nicely unless your files are really big: 除非文件很大,否则效果很好:
您应该尝试复制文件,而不是在同一文件中读写
Try this, 尝试这个,
....
StringBuilder fullText =new StringBuilder();
while((getText = br.readLine()) != null){
putText = "";
if(getText != null){
putText = getText.replaceFirst("Dog", "Cat");
}
fullText.append(putText);
}
br.close();
bw.write(putText);
bw.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.