Find a line in a file and remove it

后端 未结 16 1859
谎友^
谎友^ 2020-11-22 13:06

I\'m looking for a small code snippet that will find a line in file and remove that line (not content but line) but could not find. So for example I have in a file following

16条回答
  •  不知归路
    2020-11-22 13:44

    I refactored the solution that Narek had to create (according to me) a slightly more efficient and easy to understand code. I used embedded Automatic Resource Management, a recent feature in Java and used a Scanner class which according to me is more easier to understand and use.

    Here is the code with edited Comments:

    public class RemoveLineInFile {
    
        private static File file;
    
        public static void main(String[] args) {
            //create a new File
            file = new File("hello.txt");
            //takes in String that you want to get rid off
            removeLineFromFile("Hello");
        }
    
    
        public static void removeLineFromFile(String lineToRemove) {
    
    
            //if file does not exist, a file is created
    
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        System.out.println("File "+file.getName()+" not created successfully");
                    }
                }
    
                // Construct the new temporary file that will later be renamed to the original
                // filename.
                File tempFile = new File(file.getAbsolutePath() + ".tmp");
    
               //Two Embedded Automatic Resource Managers used
                // to effectivey handle IO Responses
              try(Scanner scanner = new Scanner(file)) {
                  try (PrintWriter pw = new PrintWriter(new FileWriter(tempFile))) {
    
                      //a declaration of a String Line Which Will Be assigned Later
                      String line;
    
                      // Read from the original file and write to the new
                      // unless content matches data to be removed.
                      while (scanner.hasNextLine()) {
                          line = scanner.nextLine();
                          if (!line.trim().equals(lineToRemove)) {
    
                              pw.println(line);
                              pw.flush();
                          }
                      }
                      // Delete the original file
                      if (!file.delete()) {
                          System.out.println("Could not delete file");
                          return;
                      }
    
                      // Rename the new file to the filename the original file had.
                      if (!tempFile.renameTo(file))
                          System.out.println("Could not rename file");
                  }
              }
            catch (IOException e)
            {
                System.out.println("IO Exception Occurred");
            }
    
        }
    
    
    
    }
    

提交回复
热议问题