如何使用 Java 中的用户输入从文本文件中删除旧数据
How to remove old data from text file using user input in Java
我正在尝试获取用户输入并查看它是否与文本文件中的任何句子相匹配。如果是这样,我想删除这句话。我的意思是,到目前为止,我已经有了搜索实现,我所需要的只是帮助删除句子并可能重写到文本文件中。我不熟悉Java。任何帮助,将不胜感激。
public static void searchFile(String s) throws FileNotFoundException {
File file = new File("data.txt");
Scanner keyboard = new Scanner(System.in);
// String lines = keyboard.nextLine();
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
// a match!
System.out.println(lineFromFile + "is found already");
System.out.println("would you like to rewrite new data?");
String go = keyboard.nextLine();
if (go.equals("yes")) {
// Here i want to remove old data in the file if the user types yes then rewrite new data to the file.
}
}
}
}
我认为你不能同时读取和写入文件,所以,创建一个临时文件并将所有数据和替换文本写入新文件,然后将该临时文件移动到原始文件。
我在下面附加了代码,希望这对您有所帮助。
File f = new File("D:\test.txt");
File f1 = new File("D:\test.out");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String s = "test";
BufferedReader br = new BufferedReader(new FileReader(f));
PrintWriter pr = new PrintWriter(f1);
String line;
while((line = br.readLine()) != null){
if(line.contains(s)){
System.out.println(line + " is found already");
System.out.println("would you like to rewrite new data?");
String go = input.readLine();
if(go.equals("yes")){
System.out.println("Enter new Text :");
String newText = input.readLine();
line = line.replace(s, newText);
}
}
pr.println(line);
}
br.close();
pr.close();
input.close();
Files.move(f1.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);
我正在尝试获取用户输入并查看它是否与文本文件中的任何句子相匹配。如果是这样,我想删除这句话。我的意思是,到目前为止,我已经有了搜索实现,我所需要的只是帮助删除句子并可能重写到文本文件中。我不熟悉Java。任何帮助,将不胜感激。
public static void searchFile(String s) throws FileNotFoundException {
File file = new File("data.txt");
Scanner keyboard = new Scanner(System.in);
// String lines = keyboard.nextLine();
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
final String lineFromFile = scanner.nextLine();
if (lineFromFile.contains(s)) {
// a match!
System.out.println(lineFromFile + "is found already");
System.out.println("would you like to rewrite new data?");
String go = keyboard.nextLine();
if (go.equals("yes")) {
// Here i want to remove old data in the file if the user types yes then rewrite new data to the file.
}
}
}
}
我认为你不能同时读取和写入文件,所以,创建一个临时文件并将所有数据和替换文本写入新文件,然后将该临时文件移动到原始文件。 我在下面附加了代码,希望这对您有所帮助。
File f = new File("D:\test.txt");
File f1 = new File("D:\test.out");
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String s = "test";
BufferedReader br = new BufferedReader(new FileReader(f));
PrintWriter pr = new PrintWriter(f1);
String line;
while((line = br.readLine()) != null){
if(line.contains(s)){
System.out.println(line + " is found already");
System.out.println("would you like to rewrite new data?");
String go = input.readLine();
if(go.equals("yes")){
System.out.println("Enter new Text :");
String newText = input.readLine();
line = line.replace(s, newText);
}
}
pr.println(line);
}
br.close();
pr.close();
input.close();
Files.move(f1.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING);