如何知道文件之前是否被处理过

发布于 2024-10-27 02:19:02 字数 168 浏览 7 评论 0原文

我如何确定文件之前是否已被处理过?有一个远程存储位置,它是我的应用程序的文件源。我的程序从该位置获取文件并按计划的方式处理它们。如何确定下次只获取未处理的文件?我正在考虑使用文件属性。存档和修改日期可以是一个解决方案。但我了解到有两位文件属性没有被使用。如何在 Java 中使用这些字段?顺便说一句,我不想​​使用数据库。

How can I be sure if a file was processed before? There is a remote storage location which is a file source for my application. My program gets files from this location and processes them in a scheduled way. How can I be sure that the next time I fetch only non-processed files? I'm thinking about using file attributes. The archive and modified date can be a solution. But I learned that two bits of file attributes are not used. How can I use these fields in Java? By the way I don't want to use a database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长安忆 2024-11-03 02:19:02

一种常见的策略是使用某种形式的哈希函数来创建校验和。记录文件的校验和,并将校验和标识的已处理文件列表与相关文件进行比较。如果有问题的文件的校验和在列表中,则您已经处理了它。

保护已处理文件校验和的列表。如果你丢失了它,或者它被损坏了,这可能是漫长而糟糕的一天。

为了防止不必要的网络流量,您可以考虑在远程存储库上准备“检查”文件,其中包含与潜在输入文件相对应的校验和。

编辑:

根据进一步评论,有可能直接与文件系统属性交互。 提议的 Java 1.7 规范引入文件系统特定的属性视图来直接与这些属性交互。您感兴趣的视图是“DosFileAttributeView”。

基本用法可能与此类似(“输入”是基于 java“路径”的文件;添加必要的异常处理):

// import as necessary from java.nio.file and java.io

DosFileAttributeView view = input.getFileAttributeView(DosFileAttributeView.class);

//Check if the system supports this view 
if (view != null) 
{
    DosFileAttributes attributes = view.readAttributes();

    // skip any file already marked as an archive
    if (!attributes.isArchive()) 
    {
         myObject.process(input)
         attributes.setArchive(true)
    }
}

A common strategy is to use some form of hash function to create a checksum. Record the checksum of the file, and compare the list of processed files identified by checksum against the file in question. If the checksum of the file in question is in the list, you have already processed it.

Protect your list of processed file checksums. If you lose it, or it becomes corrupted, it might be a long, bad day.

To prevent unnecessary network traffic, you might consider preparing 'check' files on the remote repository that contain a checksum that corresponds to a potential input file.

EDIT:

Upon further comment, it is potentially possible to directly interact with file system attributes. The proposed Java 1.7 spec introduces file-system specific attribute views to directly interact with these attributes. The view you would be interested in is 'DosFileAttributeView'.

Basic use might be something similar to this ('input' is a file based on a java 'Path'; add necessary exception handling):

// import as necessary from java.nio.file and java.io

DosFileAttributeView view = input.getFileAttributeView(DosFileAttributeView.class);

//Check if the system supports this view 
if (view != null) 
{
    DosFileAttributes attributes = view.readAttributes();

    // skip any file already marked as an archive
    if (!attributes.isArchive()) 
    {
         myObject.process(input)
         attributes.setArchive(true)
    }
}
心如荒岛 2024-11-03 02:19:02

您可以重命名该文件(例如“filename.archive”)吗?或进入“存档”子目录?

Can you rename the file (e.g. "filename.archive")? or into an "archive" subdirectory?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文