我必须创建一个应用程序来检索Web上的xml文件,并将它存储在Blackberry手机中的SD卡上。 xml文件由cron作业更新。因此,如果数据已添加到此xml文件,我希望应用程序下载新的xml文件。比较两个DataInputStream的结尾
现在我比较使用这段代码
public static boolean isSame(DataInputStream input1, DataInputStream input2) throws IOException {
boolean error = false;
try {
byte[] buffer1 = new byte[1024];
byte[] buffer2 = new byte[1024];
try {
int numRead1 = 0;
int numRead2 = 0;
while (true) {
numRead1 = input1.read(buffer1);
numRead2 = input2.read(buffer2);
if (numRead1 > -1) {
if (numRead2 != numRead1) return false;
// Otherwise same number of bytes read
if (!Arrays.equals(buffer1, buffer2)) return false;
// Otherwise same bytes read, so continue ...
} else {
// Nothing more in stream 1 ...
return numRead2 < 0;
}
}
} finally {
input1.close();
}
} catch (IOException e) {
error = true; // this error should be thrown, even if there is an error closing stream 2
throw e;
} catch (RuntimeException e) {
error = true; // this error should be thrown, even if there is an error closing stream 2
throw e;
} finally {
try {
input2.close();
} catch (IOException e) {
if (!error) throw e;
}
}
}
它完美的所有数据文件,但需要较长时间才能运行。
那么有可能读取文件的末尾来查看是否进行了更改,如果是,请重新下载它?
谢谢!例如,我将简单地创建2个或3个最后一个条目的un文件并比较它:D – bulby97