Java读取文件并删除行,无法弄清楚错误

时间:2012-04-22 18:00:17

标签: java file-io java.util.scanner

我正在开发一个项目,它有一个读取文件的方法,找到一个传入的字符串,写一个名为temp的新文件(它没有传入的行),然后删除原文并重命名temp以opriginal名称。我可以毫无错误地运行它,但它不会输出任何新文件。我已经完成了通常的调试,发现错误在于它将行写入新文件的行。我觉得我犯了一个小错误,说错了。任何解决它的帮助都会很棒......

这是代码

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);
    }
}

}

1 个答案:

答案 0 :(得分:4)

你应该在out(BufferedReader)上调用close。

你也应该在try-catch-finally的finally子句中关闭它。

您的代码应该或多或少

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);
        }
    }
}