开发者

Problem updating list of data

开发者 https://www.devze.com 2023-03-19 08:07 出处:网络
private static void deleteProxy(File proxyOld, String host, int port) { try { String lines, tempAdd; boolean removeLine = false;
private static void deleteProxy(File proxyOld, String host, int port) {
        try {
            String lines, tempAdd;
            boolean removeLine = false;
            File proxyNew = new File("proxies_" + "cleaner$tmp");
            BufferedReader fileStream = new BufferedReader(new InputStreamReader(new FileInputStream(proxyOld)));
            BufferedWriter replace = new BufferedWriter(new FileWriter(proxyNew));
            while ((lines = fileStream.readLine()) != null) {
                tempAdd = lines.trim();
                if (lines.trim().equals(host + ":" + port)) {
                    removeLine = true;
                }
                if (!removeLine) {
                    replace.write(tempAdd);
                    replace.newLine();
                }
            }
            fileStream.close();
            replace.close();
            proxyOld.delete();
            proxyNew.renameTo(proxyOld);
        } cat开发者_开发问答ch (Exception e) {
            e.printStackTrace();
        }
    }

Calling the function:

File x = new File("proxies.txt");//is calling a new file the reason why it's being flushed out?
deleteProxy(x, host, port);

Before I run the program the file proxies.txt had data inside of it. However when I run the program it appears to be flushed out. It becomes empty.

I noticed while the program is running, if I move my mouse over the file proxies.txt, Windows displays the "Date Modified" and the time it displays is the current time, or last time the function deleteProxy(...) was executed.

Does anyone know what I'm doing wrong? And why won't the list update instead of appearing to be empty?

Updated code:

private static void deleteProxy(File proxyOld, String host, int port) {
        try {
            String lines, tempAdd;
            boolean removeLine = false;
            File proxyNew = new File("proxies_" + "cleaner$tmp");
            FileInputStream in = new FileInputStream(proxyOld);
            InputStreamReader read = new InputStreamReader(in);
            BufferedReader fileStream = new BufferedReader(read);
            FileWriter write = new FileWriter(proxyNew);
            BufferedWriter replace = new BufferedWriter(write);

            while ((lines = fileStream.readLine()) != null) {
                tempAdd = lines.trim();
                if (lines.trim().equals(host + ":" + port)) {
                    removeLine = true;
                }
                if (!removeLine) {
                    replace.write(tempAdd);
                    replace.newLine();
                }
            }
            in.close();
            read.close();
            fileStream.close();
            write.close();
            replace.close();

            if (proxyOld.delete()) {
                throw new Exception("Error deleting " + proxyOld);
            }

            if (proxyNew.renameTo(proxyOld)) {
                throw new Exception("Error renaming " + proxyOld);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Running the updated code it deletes proxies.txt just fine but it fails to make the new file:\ Maybe I should find a new method to update a text file, do you have any suggestions?


Your rename operation may not work, as per the File.renameTo() documentation:

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

So basically, you're wiping your old file, and you're not guaranteed the new file will take its place. You must check the return value of File.renameTo():

if(proxyNew.renameTo(proxyOld)){
    throw new Exception("Could not rename proxyNew to proxyOld");
}

As for why your renameTo may be failing: you're not closing the nested set of streams that you open to read from the old file, so the operating system may still consider an abstract pathname to exist. Try making sure you close all of the nested streams you open.


Try this:

FileInputStream in = new FileInputStream(proxyOld);
BufferedReader fileStream = new BufferedReader(new InputStreamReader(in));

...

in.close();
0

精彩评论

暂无评论...
验证码 换一张
取 消