1 回答

TA貢獻1998條經驗 獲得超6個贊
據我現在的理解,這個問題是無關緊要的。我想要的是一種壓縮數據的方法,因為我的內存無法處理它。但現在我意識到,我應該做的不是那樣,而是分塊讀取文件,所以壓縮與否并不重要。這是怎么做的:
try {
? ? File file = new File("myFile");
? ? FileInputStream is = new FileInputStream(file);
? ? byte[] chunk = new byte[1024];
? ? int chunkLen = 0;
? ? while ((chunkLen = is.read(chunk)) != -1) {
? ? ? ? // your code..
? ? }
} catch (FileNotFoundException fnfE) {
? ? // file not found, handle case
} catch (IOException ioE) {
? ? // problem reading, handle case
}
至于壓縮函數,如果有人想要的話,可以使用由tonyking97編寫的這個很棒的LZW 壓縮算法,基本上你必須將字節數組轉換為字符串,然后通過這個壓縮函數傳遞它。它可以工作,但是對于大字節數組來說速度很慢。
當然,為了再次寫入文件,我們必須使用而FileOutputStream
不是FileInputStream并說outputStream.write(byte[], int, int)
.?希望這會對某人有所幫助:)
添加回答
舉報