3

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

这是代码

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

}

4

1 回答 1

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);
        }
    }
}
于 2012-04-22T18:05:46.533 回答