Re: BufferedReader一問

看板java作者時間19年前 (2006/01/19 11:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/2 (看更多)
※ 引述《ufjl1785 (David)》之銘言: > 我有一個七千行的檔案 > 但是我每讀一次僅僅讀其中的某兩行(有指定行數) > 請問要怎樣讀才能快速地讀到這兩行 > 我發現好像都沒啥效率, > 不知道有沒有比較快的方法? > (不要跟我講讀到記憶體裡面...Orz) 做了簡單的實驗:) ============================================================================ test 1:: inputstream 828ms 828ms 813ms 813ms 828 ms ---------------------- package other; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class ReadUsingInputStream { public static void main(String[] args) { try { File f = new File("testData.txt"); FileInputStream fis = new FileInputStream(f); int lineCount = 0; int selectLine = 7000; StringBuffer strBuffer = new StringBuffer(); char c = '\0'; long t1 = System.currentTimeMillis(); do { c = (char) fis.read(); if (c == '\n') { lineCount++; } } while (lineCount != selectLine - 1 && selectLine != 0 && selectLine - 1 <= 7000); c = (char) fis.read(); strBuffer.append(c); while (c != '\n') { c = (char) fis.read(); strBuffer.append(c); } System.out.println(System.currentTimeMillis()-t1); System.out.println(strBuffer); fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ============================================================================ test 2:: buffered reader 46ms 31ms 32ms 47ms 63ms ---------------------- package other; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class ReadUsingBufferedReader { public static void main(String[] args) { BufferedReader reader; int lineCount = 0; int selectLine = 7000; String str; try { reader = new BufferedReader(new FileReader("testData.txt")); long t1 = System.currentTimeMillis(); do { lineCount++; reader.readLine(); } while (lineCount != selectLine-1 && selectLine != 0 && selectLine-1 <= 7000); str = reader.readLine(); System.out.println(System.currentTimeMillis()-t1); reader.close(); //System.out.println(str); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } ============================================================================ test 3:: file mapping 16ms 15ms 16ms 0ms 16ms ---------------------- package other; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel.MapMode; public class ReadUsingFileChannel { public static void main(String[] args) { try { File f = new File("testData.txt"); FileInputStream fis = new FileInputStream(f); MappedByteBuffer mappedfile = fis.getChannel().map( MapMode.READ_ONLY, 0, f.length()); int lineCount = 0; int selectLine = 7000; StringBuffer strBuffer = new StringBuffer(); char c = '\0'; long t1 = System.currentTimeMillis(); do { c = (char) mappedfile.get(); if (c == '\n') { lineCount++; } } while (lineCount != selectLine - 1 && selectLine != 0 && selectLine - 1 <= 7000); c = (char) mappedfile.get(); strBuffer.append(c); while (c != '\n') { c = (char) mappedfile.get(); strBuffer.append(c); } System.out.println(System.currentTimeMillis()-t1); System.out.println(strBuffer); fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } -- ※ Origin: SayYA 資訊站 <bbs.sayya.org> ◆ From: 163.26.34.214 ◆ Modify: 06/01/19 10:59:14 <163.26.34.214>
文章代碼(AID): #13pmWs00 (java)
討論串 (同標題文章)
文章代碼(AID): #13pmWs00 (java)