java 如何从Java中的文本文件中删除一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14646340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a line from text file in Java
提问by Vijay Kumar
I am creating one application in Java swing api. I am using txt file as my database to store,update etc. I completed storing procedure successfully. But I cant update .If I click update button on my application the data in file could not be replaced. can you please tel me how to replace a data in file?
我正在用 Java swing api 创建一个应用程序。我使用 txt 文件作为我的数据库来存储、更新等。我成功地完成了存储过程。但我无法更新。如果我点击我的应用程序上的更新按钮,文件中的数据无法被替换。你能告诉我如何替换文件中的数据吗?
Thanks in advance...
提前致谢...
import java.io.*;
public class test {
public static void main(String args[]) {
try {
String data = null;
File file = new File("student.txt", true);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
while ((data = br.readLine()) != null) {
String[] de = data.split(" ");
if (de[0].equals("vimal")) {
data.trim();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
回答by M.Kreusburg
you need to close your buffered reader after all and in your fileReader need an "True"
毕竟你需要关闭你的缓冲阅读器,并且在你的 fileReader 中需要一个“True”
import java.io.*;
public class test {
public static void main(String args[]) {
try {
String data= null;
File file=new File("student.txt");
FileReader fr =new FileReader(file,true);
BufferedReader br = new BufferedReader(fr);
while((data=br.readLine())!= null) {
String[] de = data.split(" ");
if(de[0].equals("vimal")) {
data.trim();
}
}
} catch (IOException e) {
e.printStackTrace();
}
br.close()
}
}