File getFreeSpace acts strange

时间:2016-03-02 10:51:51

标签: java jdk1.6

I have an application, which runs on Tomcat 6, and uses JDK 1.6.0_34.

Periodically (for about every 1-5 minutes) it checks a shared folder, and do some actions (no content delete, note) if there is no free space on a hard drive. So, usually everything is ok, but for about once a day File getFreeSpace method return 0, even while there is a lot of free space on a hard drive.

In my code I have this:

public void toDo() {
    File file = new File(FOLDER_PATH);
    long space = file.getFreeSpace();
    long minDiskSize = settings.getMinDiskSize();
    String absolutePath = file.getAbsolutePath();
    if (space < minDiskSize) {
        log.debug("NOT_OK:\n" +
                "Folder path: [" + absolutePath + "]\n" +
                "Min free space from config: [" + minDiskSize + "]\n" +
                "Free space on server: [" + space + "]");
        //Do some actions
    } else {
        log.debug("OK:\n" +
                "Folder path: [" + absolutePath + "]\n" +
                "Min free space from config: [" + minDiskSize + "]\n" +
                "Free space on server: [" + space + "]");
    }
}

Here is logs:

2016-03-02 13:35:22,810 <- note, time from a log file.
NOT_OK:
Folder path: [\\server\FOLDER$\input]
Free space from config: [104857600]
Free space on server: [0]

2016-03-02 13:36:24,029 <- note, just a minute latter everything is OK again.
OK:
Folder path: [\\server\FOLDER$\input]
Min free space from config: [104857600]
Free space on server: [402494513152]

So, what can be a cause of such strange behavior?

1 个答案:

答案 0 :(得分:1)

这可能只是一个小故障,所以, 我建议在空间小于minDiskSize时仔细检查空间 。我的意思是这样的:

public void toDo() {
File file = new File(FOLDER_PATH);
long space = file.getFreeSpace();
long minDiskSize = settings.getMinDiskSize();
String absolutePath = file.getAbsolutePath();
if (space < minDiskSize) {
Thread.sleep(62000);
space = file.getFreeSpace();
 if(space < minDiskSize){
    log.debug("NOT_OK:\n" +
            "Folder path: [" + absolutePath + "]\n" +
            "Min free space from config: [" + minDiskSize + "]\n" +
            "Free space on server: [" + space + "]");
    //Do some actions
   }else{log.debug("OK:\n" +
            "Folder path: [" + absolutePath + "]\n" +
            "Min free space from config: [" + minDiskSize + "]\n" +
            "Free space on server: [" + space + "]");}
} else {
    log.debug("OK:\n" +
            "Folder path: [" + absolutePath + "]\n" +
            "Min free space from config: [" + minDiskSize + "]\n" +
            "Free space on server: [" + space + "]");
}
}

希望这会有所帮助。