English 中文(简体)
如何测试与 Java的“完整”(完整书写)
原标题:How to test if a file is "complete" (completely written) with Java
  • 时间:2012-04-05 13:18:01
  •  标签:
  • java

请允许我说,你有一份外部程序文件给某些名录,你定期尝试从该名录中读到档案。 避免的问题在于阅读一个文件,即其他程序目前处于撰写过程的中间阶段,因此不完整。 目前,采用最低档案年龄检查程序,因此忽视了所有档案,除非其最后修订日期超过XX秒。

我很想知道,是否有更清洁的办法解决这一问题。 如果档案类型不为人所知(可能有若干不同格式),是否有可靠方法对档案目录进行核对,以了解应当列入档案的 by的数量,以及目前档案中用以确认其匹配的 by的数量?

感谢任何想法或想法!

最佳回答

你可以使用外部标识文档。 撰写过程可以产生一个文件XYZ。 在此之前,它开始编造XYZ,删除XYZ。 XYZ完成后24小时。 然后,读者很容易知道,只有在相应的锁档案不存在的情况下,它才能认为档案完整。

问题回答

The way I ve done this in the past is that the process writing the file writes to a "temp" file, and then moves the file to the read location when it has finished writing the file.

因此,写作过程将写到info.txt.tmp。 完工后,档案改名为info.txt。 然后,阅读过程必须检查info.txt的存在,而且它知道,如果存在的话,那是完全书写的。

或者,如果你不喜欢使用传闻的档案延期,你可以将书面程序写到另一个名录上。

我别无选择,因为客户正在把档案上载到主要的巴普里尔人基金会。 它们的规模可能非常大。

它很 ha,但我比较了在睡觉前和睡觉之后的档案规模。

它显然不宜打脚,但就我们而言,它只是作为背景系统进程运行,所以似乎工作得当。

private boolean isCompletelyWritten(File file) throws InterruptedException{
    Long fileSizeBefore = file.length();
    Thread.sleep(3000);
    Long fileSizeAfter = file.length();

    System.out.println("comparing file size " + fileSizeBefore + " with " + fileSizeAfter);

    if (fileSizeBefore.equals(fileSizeAfter)) {
        return true;
    }
    return false;
}

注:如下文所述,这可能不会在窗户上工作。 这在含水层环境中使用。

以往用于“Windows”这一假设情景的简单解决办法一是使用boolean file.renameTo(File),并试图将原始档案移至单独的编组:

boolean success = potentiallyIncompleteFile.renameTo(stagingAreaFile);

如果success is false,那么potentiallyIncompleteFile 仍在写上。

即便是 by的数量相同,档案内容也可能不同。

因此,我认为,你必须按部就地对旧档案和新档案进行对比。

似乎解决这一问题的两种选择:

  1. the best option- writer process notify reading process somehow that the writing was finished.
  2. write the file to {id}.tmp, than when finish- rename it to {id}.java, and the reading process run only on *.java files. renaming taking much less time and the chance this 2 process work together decrease.

第一, 为什么在复制Samba股份时,X号投放像窗户之类的文件?,但这改变了你已经做些什么。

就阅读任意档案和寻找规模而言,有些档案有这种信息,有些没有,甚至没有代表这种资料的共同方法。 你们需要每种格式的具体信息,并各自独立管理。

如果你绝对必须在档案中行事,那么你的书面程序将需要发出某种通知。 否则,与随机档案中的随机区块相比,你 re忙地抽取了文件,读书单价格相当低。

One more method to test that a file is completely written:

private void waitUntilIsReadable(File file) throws InterruptedException {
    boolean isReadable = false;
    int loopsNumber = 1;
    while (!isReadable && loopsNumber <= MAX_NUM_OF_WAITING_60) {
        try (InputStream in = new BufferedInputStream(new FileInputStream(file))) {
            log.trace("InputStream readable. Available: {}. File:  {} ",
                    in.available(), file.getAbsolutePath());
            isReadable = true;
        } catch (Exception e) {
            log.trace("InputStream is not readable yet. File:  {} ", file.getAbsolutePath());
            loopsNumber++;
            TimeUnit.MILLISECONDS.sleep(1000);
        }
    }
}

Use this for Unix if you are transferring files using FTP or Winscp:

public static void isFileReady(File entry) throws Exception {
        long realFileSize = entry.length();
        long currentFileSize = 0;
        do {
            try (FileInputStream fis = new FileInputStream(entry);) {
                currentFileSize = 0;
                while (fis.available() > 0) {
                    byte[] b = new byte[1024];
                    int nResult = fis.read(b);
                    currentFileSize += nResult;
                    if (nResult == -1)
                        break;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("currentFileSize=" + currentFileSize + ", realFileSize=" + realFileSize);
        } while (currentFileSize != realFileSize);
    }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签