我想通過在Java中使用FileInputStream一次讀取塊來讀取文件。 的代碼如下:在讀取文件時運行和調試時出現不同的輸出
File file = new File(filepath);
FileInputStream is = new FileInputStream(file);
byte[] chunk = new byte[bytelength];
int chunkLen = chunk.length;
long lineCnt = 0;
while ((chunkLen = is.read(chunk)) != -1) {
String decoded = getchunkString(chunk);
System.out.println(decoded);
System.out.println("---------------------------------");
}
我正在bytelength = 128,並試圖用較小的文件進行測試,如下所示:
graph G{
biz -- mai
biz -- ded
biz -- ronpepsi
blaine -- dan
dan -- graysky
dan -- iancr
dan -- maxwell
dan -- foursquare
blaine -- neb
}
當我運行的代碼讀取塊這樣的:
graph G{
biz -- mai
biz -- ded
biz -- ronpepsi
blaine -- dan
dan -- graysky
dan -- iancr
dan -- maxwell
dan -- foursquare
blaine
---------------------------------
-- neb
}
iz -- mai
biz -- ded
biz -- ronpepsi
blaine -- dan
dan -- graysky
dan -- iancr
dan -- maxwell
dan -- foursquare
blaine
---------------------------------
我不明白第二塊是怎麼來的?我希望它應該是唯一的
-- neb
}
當我debugg is.read(chunk)
變爲10,然後-1,只會打印第一個塊。
如果你正在閱讀的文本文件,你可以使用的FileReader(裹在BufferedReader類) – 2013-03-19 05:54:21