Java read file and delete line, cant find out the error

I am working on a project that has a method that reads a file, finds the line aa, which is passed to the new file's entry named temp (which does not have a line in it), and then deletes the original and renames temp to the original name. I can run it without errors, but it doesn’t output anything to the new file. I usually performed debugging and found that the error lies in the lines where it writes a line to a new file. I feel like I made a small mistake saying something was wrong. Any help resolving it would be great ...

Here is the code

public static void LineDelete(String Filename, String Content) throws IOException {
    try {
        File flights = new File("AppData/" + Filename);
        File temp;
        temp = new File("AppData/temp.txt");
        FileWriter fstream;
        BufferedWriter out;
        try (Scanner sc = new Scanner(flights)) {

            fstream = new FileWriter("AppData/temp.txt", true);
            out = new BufferedWriter(fstream);
            boolean exis = temp.exists();
            if (exis) {
                temp.delete();
                temp = new File("AppData/temp.txt");
                boolean createNewFile = temp.createNewFile();
            } else {
                boolean creatNewFile = temp.createNewFile();
            }
            String f;
            while (sc.hasNextLine()) {
                f = sc.nextLine();
                if (!f.equals(Content)) {

                    out.newLine();
                    out.write(f);

                }

            }
        }
        fstream.close();
        //out.close();
        flights.delete();
        File flightsn = new File("AppData/" + Filename);
        temp.renameTo(flightsn);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

+3
source share
1 answer

You must call close on out(BufferedReader).

You should also close it in the finally clause in try-catch-finally.

Your code should be more or less

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileWrite {
    public static void LineDelete(String Filename, String Content)
            throws IOException {
        BufferedWriter out = null;
        File flights = new File("AppData/" + Filename);
        File temp = new File("AppData/temp.txt");
        FileWriter fstream = null;

        try {
            Scanner sc = new Scanner(flights);
            fstream = new FileWriter("AppData/temp.txt", true);
            out = new BufferedWriter(fstream);
            boolean exis = temp.exists();
            if (exis) {
                temp.delete();
                temp = new File("AppData/temp.txt");
                boolean createNewFile = temp.createNewFile();
            } else {
                boolean creatNewFile = temp.createNewFile();
            }
            String f;
            while (sc.hasNextLine()) {
                f = sc.nextLine();
                if (!f.equals(Content)) {
                    out.newLine();
                    out.write(f);
                }

            }
        } catch (IOException ex) {
            Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (Exception e) {
                Logger.getLogger(FileWrite.class.getName()).log(Level.SEVERE, null, e);
            }
        }

        if(flights.exists()){           
            flights.delete();
            File flightsn = new File("AppData/" + Filename);
            temp.renameTo(flightsn);
        }
    }
}
+4
source

All Articles