从属性文件中删除记录

时间:2014-04-17 11:25:16

标签: java io

我正在尝试从属性文件中删除一些记录,但我的代码会删除所有记录。我做错了什么?

import java.io.*;


public class FileCheck {

    private final String HOST_KEY = "database.host";
    private final String SID_KEY = "database.sid";
    private final String USER_KEY = "database.user";
    private final String PASS_KEY = "database.pass";
    private final String FROMEMAIL_KEY = "fromemailid";
    private final String FROMEMPASS_KEY = "fromemailpass";
    private final String USETLS_KEY = "usetls";
    private final String MAILSERVER_KEY = "emailserverhost";
    private final String OUTGOINGPORT_KEY = "outgoingemailport";

    public FileCheck() {

    }
 public void removeEditableProperties() {
        BufferedReader reader;
        BufferedWriter writer;
        try {
            File inputFile = new File("BiDBProperties.properties");
            File tempFile = new File("BiDBProperties_temp.properties");

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

            while ((currentLine = reader.readLine()) != null) {
                String trimmedLine = currentLine.trim();

                if (trimmedLine.startsWith(HOST_KEY) || trimmedLine.startsWith(SID_KEY)
                        || trimmedLine.startsWith(USER_KEY) || trimmedLine.startsWith(PASS_KEY)
                        || trimmedLine.startsWith(FROMEMAIL_KEY)
                        || trimmedLine.startsWith(FROMEMPASS_KEY) || trimmedLine.startsWith(USETLS_KEY)
                        || trimmedLine.startsWith(MAILSERVER_KEY) || trimmedLine.startsWith(OUTGOINGPORT_KEY)) {
                    continue;
                }
                writer.write(currentLine);
                writer.newLine();
                writer.flush();
            }
            writer.close();
            reader.close();

            if (!inputFile.delete()) {
                System.out.println("ERROR: Operation Failed. Please Try again");
                return;
            }
            if (!tempFile.renameTo(inputFile)) {
                System.out.println("ERROR: Operation Failed. Please Try again");
            }

        } catch (IOException asd) {
            System.out.println(asd.getMessage());
        }
    }

}

我有什么问题吗?

1 个答案:

答案 0 :(得分:2)

它非常适合我..从BiDBProperties.properties文件中删除所需的属性。请在运行程序之前关闭这两个文件并重试