看板java
标 题Re: BufferedReader一问
发信站SayYA 资讯站 (Thu Jan 19 10:59:14 2006)
转信站ptt!ctu-reader!news.nctu!SayYa
※ 引述《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>