在java IO中读取包含单行字符串的大型文件的最佳方法

在java IO中读取包含单行字符串的大型文件的最佳方法,java,io,Java,Io,我做了一个小实验来读取包含单行字符串和多行字符串的文件。单线文件的长度为198890,多线文件的长度为208890。我用以下六种方法对它们进行了测试,得到了它们读取的时间和字符串长度。这里我已经提到了测试方法、结果和实现。 我实际考虑的是读取一个包含单行文本的大文件。根据结果,IO utils似乎比其他产品更好那么,除了我在下面实现的方法(如果有的话),我可以使用什么样的最佳方法来实现这一点呢 结果:(计时单位为秒。0表示不到一秒:) 试验方法: public void iOTester(){

我做了一个小实验来读取包含单行字符串和多行字符串的文件。单线文件的长度为198890,多线文件的长度为208890。我用以下六种方法对它们进行了测试,得到了它们读取的时间和字符串长度。这里我已经提到了测试方法、结果和实现。

我实际考虑的是读取一个包含单行文本的大文件。根据结果,IO utils似乎比其他产品更好

那么,除了我在下面实现的方法(如果有的话),我可以使用什么样的最佳方法来实现这一点呢

结果:(计时单位为秒。0表示不到一秒:)

试验方法:

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");


    }
方法2:(ReadWithBufferedReaderToCharray)

方法3:(ReadWithStreamToByteArray)

方法4:(ReadWithStreamToByteArrayChunks)

方法5:(从ApacheFileUtils读取)

方法6:(从APACHEIOUTILS读取)

参考文献:

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");


    }

您也可以测试此方法

String text = new String(Files.readAllBytes(Paths.get(path)));
还有带直接缓冲区的文件通道

    FileChannel fc = FileChannel.open(path);
    ByteBuffer buf = ByteBuffer.allocateDirect((int)fc.size());
    fc.read(buf);

使用JavaVM参数-d64-Xmx6G,在64位配置中可以将容量增加到6GB

 IOUtils.toString((InputStream) new FileInputStream("C://exocerebro//userrecord.sql"), "UTF-8");

基准测试是否考虑了磁盘缓存和JIT编译的影响?谢谢Evgeny。我也会试试。只从Java7开始(文件类)
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();
String text  = new String(FileUtils.readFileToByteArray(new File(filePath)));
String text = new String(IOUtils.toByteArray(new FileInputStream(filePath)));
String text = new String(Files.readAllBytes(Paths.get(path)));
    FileChannel fc = FileChannel.open(path);
    ByteBuffer buf = ByteBuffer.allocateDirect((int)fc.size());
    fc.read(buf);
 IOUtils.toString((InputStream) new FileInputStream("C://exocerebro//userrecord.sql"), "UTF-8");