简体   繁体   中英

How can I delete lines of data in textfile using java? eg. my textfile is data.txt

From read the line needed to be deleted by the u ser to delete it

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Delete {

    public static void main(String[] args) throws IOException {

        File input = new File("data.txt");
        FileReader fr = null;
        Scanner ob = new Scanner(System.in);

        // declare variable
        String DeleteWord, str, newDeleteWord;
        System.out.print("Enter word you want to delete: ");
        DeleteWord = ob.nextLine();
        newDeleteWord = (capitalize(DeleteWord));

        try {
            fr = new FileReader(input);
            BufferedReader br = new BufferedReader(fr);

            while ((str = br.readLine()) != null) {
                if (str.contains(newDeleteWord)) {
                    System.out.println(str);
                }

                Scanner read = new Scanner(System.in);
                int selection;
                System.out.println("Confirm to delete his/her data?\n 1 for yes\n 2 for no");
                selection = read.nextInt();

                if (selection == 1)
                    if (str.contains(newDeleteWord)) {
                        str = "";
                    }
            }
        } finally {
            fr.close();
        }
    }

    public static String capitalize(String str1) {
        if (str1 == null || str1.isEmpty()) {
            return str1;
        }
        return str1.substring(0, 1).toUpperCase() + str1.substring(1);
    }
}

How can I delete lines of data in textfile using java? eg. my textfile is data.txt

This is a possible solution:

File inputFile = new File("myFile.txt"); // File which we will read
File tempFile = new File("myTempFile.txt"); // The temporary file where we will write

BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

String lineToRemove = yourString; // here is your line to remove
String currentLine;

while((currentLine = reader.readLine()) != null) {
    
    String trimmedLine = currentLine.trim(); // we trim it and remove unecessary spaces
    if(trimmedLine.equals(lineToRemove)) continue; // If it is equal to our line to remove then do not write it to our file!
    writer.write(currentLine + System.getProperty("line.separator"));
}
writer.close(); 
reader.close(); 
inputFile.delete(); // we delete the file that we have so that we have no conflicts
boolean successful = tempFile.renameTo(inputFile);

OR

Reading all the lines in a list and filtering this list.

The quickest way is through Apache Commons-IO ( or you can implement it yourself)

Apache Commons:

List<String> lines = FileUtils.readLines(file);
List<String> updatedLines = lines.stream().filter(s -> !s.contains(searchString)).collect(Collectors.toList());
FileUtils.writeLines(file, updatedLines, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM