请允许我说,你有一份外部程序文件给某些名录,你定期尝试从该名录中读到档案。 避免的问题在于阅读一个文件,即其他程序目前处于撰写过程的中间阶段,因此不完整。 目前,采用最低档案年龄检查程序,因此忽视了所有档案,除非其最后修订日期超过XX秒。
我很想知道,是否有更清洁的办法解决这一问题。 如果档案类型不为人所知(可能有若干不同格式),是否有可靠方法对档案目录进行核对,以了解应当列入档案的 by的数量,以及目前档案中用以确认其匹配的 by的数量?
感谢任何想法或想法!
请允许我说,你有一份外部程序文件给某些名录,你定期尝试从该名录中读到档案。 避免的问题在于阅读一个文件,即其他程序目前处于撰写过程的中间阶段,因此不完整。 目前,采用最低档案年龄检查程序,因此忽视了所有档案,除非其最后修订日期超过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.
因此,写作过程将写到
或者,如果你不喜欢使用传闻的档案延期,你可以将书面程序写到另一个名录上。
我别无选择,因为客户正在把档案上载到主要的巴普里尔人基金会。 它们的规模可能非常大。
它很 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的数量相同,档案内容也可能不同。
因此,我认为,你必须按部就地对旧档案和新档案进行对比。
似乎解决这一问题的两种选择:
第一, 为什么在复制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);
}
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 ...
Check this, List<String> list = new ArrayList<String>(); for (int i = 0; i < 10000; i++) { String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
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 ...
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 ...
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....
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 ...