我必須創建一個應用程序來檢索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