Best way to read a large file containing a single
I did a small experiment for the Reading files having single line string and multiple line strings. The singleLine file is 198890 lengthed and the multiLine file is 208890 lengthed. I tested them with six methods as follows and got the timings and String length that they have read. Here I've mentioned the Test method, Result and the Implementations.
My actual consideration is reading a large file contain single line text in it. According to the results it looks like IO utils is better than others.
So, what is the best way that I can use for this other than the methods that I've implemented below (if any).
Results : (Timings are in seconds. 0 means less than a sec :))
iOTest(). : Single Line Test...
singleStr.txt is deleted!
writeToFile().198890 lenghted String wrote to the file
[ReadWithBufferedReaderByLine] Text length: 198890, Total time: 18
[ReadWithBufferedReaderToCharArray] Text length: 204800, Total time: 8
[ReadWithStreamToByteArray] Text length: 198890, Total time: 8
[ReadWithStreamToByteArrayChunks] Text length: 1950, Total time: 1
[ReadFromApacheFileUtils] Text length: 198890, Total time: 30
[ReadFromApacheIOUtils] Text length: 198890, Total time: 1
iOTest(). : Multi Line Test...
multiStr.txt is deleted!
writeToFile().208890 lenghted String wrote to the file
[ReadWithBufferedReaderByLine] Text length: 198890, Total time: 15
[ReadWithBufferedReaderToCharArray] Text length: 212992, Total time: 2
[ReadWithStreamToByteArray] Text length: 208890, Total time: 1
[ReadWithStreamToByteArrayChunks] Text length: 2040, Total time: 2
[ReadFromApacheFileUtils] Text length: 208890, Total time: 0
[ReadFromApacheIOUtils] Text length: 208890, Total time: 1
Test Method :
public void iOTester(){
System.out.println("niOTester(). : Single Line Test...");
String testStr = "";
for(int i = 0; i < 10000; i++) testStr += "[Namal"+i+"Fernando] ";
writeToFile("singleStr.txt", testStr);
readWithBufferedReaderByLine("singleStr.txt");
readWithBufferedReaderToCharArray("singleStr.txt");
readWithStreamToByteArray("singleStr.txt");
readWithStreamToByteArrayChunks("singleStr.txt");
readFromApacheFileUtils("singleStr.txt");
readFromApacheIOUtils("singleStr.txt");
System.out.println("niOTester(). : Multi Line Test...");
testStr = "";
for(int i = 0; i < 10000; i++) testStr += "[Namal"+i+"Fernando] n";
writeToFile("multiStr.txt", testStr);
readWithBufferedReaderByLine("multiStr.txt");
readWithBufferedReaderToCharArray("multiStr.txt");
readWithStreamToByteArray("multiStr.txt");
readWithStreamToByteArrayChunks("multiStr.txt");
readFromApacheFileUtils("multiStr.txt");
readFromApacheIOUtils("multiStr.txt");
}
Implementations :
Method 1 : (ReadWithBufferedReaderByLine)
BufferedReader br = new BufferedReader(new
FileReader(file));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line);
}
String text = sb.toString();
Method 2 : (ReadWithBufferedReaderToCharArray)
BufferedReader br = new BufferedReader(new
FileReader(file));
StringBuilder sb = new StringBuilder();
char[] chars = new char[8192];
for(int len; (len = br.read(chars)) > 0;) {
sb.append(String.valueOf(chars));
}
String text = sb.toString();
Method 3 : (ReadWithStreamToByteArray)
InputStream is = new FileInputStream(file);
byte[] b = new byte[is.available()];
is.read(b);
String text = new String(b);
Method 4 : (ReadWithStreamToByteArrayChunks)
InputStream is = new FileInputStream(file);
byte[] b = new byte[1024];
StringBuilder sb = new StringBuilder();
int read;
while((read = is.read(b)) != -1){
sb.append(String.valueOf(b));
}
String text = sb.toString();
Method 5 : (ReadFromApacheFileUtils)
String text = new String(FileUtils.readFileToByteArray(new File(filePath)));
Method 6: (ReadFromApacheIOUtils)
String text = new String(IOUtils.toByteArray(new FileInputStream(filePath)));
References :
You can also test this method
String text = new String(Files.readAllBytes(Paths.get(path)));
and also FileChannel with direct buffer
FileChannel fc = FileChannel.open(path);
ByteBuffer buf = ByteBuffer.allocateDirect((int)fc.size());
fc.read(buf);
链接地址: http://www.djcxy.com/p/78412.html
下一篇: 读取包含单个文件的大文件的最佳方法