我有两个java程序
1)第一个程序每天运行并生成文件abc_currenttimestamp.csv(例如abc_1357615107)
2)第二个程序每天运行并读取第一个程序生成的文件。但它必须读取的文件是前几天文件,而不是今天生成的文件。
例如,第一个程序在1月9日和1月10日生成文件等。 第二个程序从10日开始运行,应该读取时间戳为1月9日的文件。
一种方法可能是第二个程序检查所有文件的时间戳,并查找它是否是前一天的时间戳。我不知道如何用Java做到这一点。
另一种方法可能是第一个程序将文件名写入字段,第二个程序通过读取该字段来知道文件名。但问题是,每当第一个程序每天运行时,它都会用今天的文件名覆盖这个字段。
谢谢!
答案 0 :(得分:0)
你可以简单地玩
file.lastModified();
或者您只需在文件名中添加日期和时间,例如ddmmyyyy.csv
,然后您就可以轻松检查它是否存在。
答案 1 :(得分:0)
我喜欢从文件名中获取时间戳的想法。这样,有人手动修改文件不会搞砸你的过程。
这取决于您拥有的时间戳值。如果它的java使用下面的代码,如果它的unix乘以1000,那么就这样做。
Calendar today = Calendar.getInstance(); // use this to get todays date (and time)
Calendar mydate = Calendar.getInstance(); // this uses the timestamp for comparing
mydate.setTimeInMillis(timestamp);
// This prints out the date and shows how to get values to compare. You can also
// subtract one day from today's date to handle rollover of months and years.
out.println( mydate.get(Calendar.DAY_OF_MONTH)+"."+mydate.get(Calendar.MONTH)+"."+mydate.get(Calendar.YEAR));
这里有一些讨论: