亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用RandomAccessFile和BufferedReader加速文件讀取

使用RandomAccessFile和BufferedReader加速文件讀取

九州編程 2019-04-20 15:15:27
我必須 :-逐行讀取大文本文件。記下每行讀取后的文件指針位置。如果運行時間大于30秒,則停止讀取文件。從新進程中的最后一個文件指針恢復。我在做什么 :使用RandomAccessFile.getFilePointer()來記錄文件指針。將RandomAccessFile包裝到另一個BufferedReader中,以根據此答案加快文件讀取過程。當時間大于30秒時,我停止讀取文件。使用新的RandomAccessFile重新啟動進程并使用RandomAccessFile.seek方法將文件指針移動到我離開的位置。問題:當我正在閱讀包裹在RandomAccessFile中的BufferedReader時,似乎文件指針在一次調用BufferedReader.readLine()時正在向前移動。但是,如果我直接使用RandomAccessFile.readLine(),文件指針正向一步一步正向移動。使用BufferedReader作為包裝器:    RandomAccessFile randomAccessFile = new RandomAccessFile("mybigfile.txt", "r");BufferedReader brRafReader = new BufferedReader     (new FileReader(randomAccessFile.getFD()));while((line = brRafReader.readLine()) != null) {     System.out.println(line+", Position : "+randomAccessFile.getFilePointer());}輸出:Line goes here, Position : 13040Line goes here, Position : 13040Line goes here, Position : 13040Line goes here, Position : 13040使用Direct RandomAccessFile.readLine    RandomAccessFile randomAccessFile = new RandomAccessFile("mybigfile.txt", "r");while((line = randomAccessFile.readLine()) != null) {     System.out.println(line+", Position : "+randomAccessFile.getFilePointer());}輸出:(這是預期的。每次調用readline時文件指針都正常移動)Line goes here, Position : 11011Line goes here, Position : 11089Line goes here, Position : 12090Line goes here, Position : 13040誰能告訴我,我在這做什么錯?有什么辦法可以使用RandomAccessFile加快閱讀過程嗎?
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 1844 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號