我必须创建一个应用程序来检索 Web 上的 xml 文件,并将其存储在黑莓手机的 SD 卡上。xml 文件由 cron 作业更新。因此,如果数据已添加到此 xml 文件中,我希望应用程序下载新的 xml 文件。
现在我使用这段代码比较所有数据文件
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;
}
}
}
它运行良好,但需要很长时间才能运行。
那么是否可以读取文件的末尾以查看是否进行了更改,如果是,请重新下载?