gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:53:58 24 4
gpt4 key购买 nike

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

我的实际考虑是读取其中包含单行文本的大文件。根据结果​​看来 IO utils 比其他的要好。

那么,除了我在下面实现的方法(如果有)之外,我可以使用的最佳方法是什么。

结果:(时间以秒为单位。0 表示小于一秒:))

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

测试方法:

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


}

实现:

方法 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();

方法 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();

方法 3:(ReadWithStreamToByteArray)

InputStream     is          = new FileInputStream(file);
byte[] b = new byte[is.available()];
is.read(b);
String text = new String(b);

方法 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();

方法 5:(ReadFromApacheFileUtils)

String text  = new String(FileUtils.readFileToByteArray(new File(filePath)));

方法 6:(ReadFromApacheIOUtils)

String text = new String(IOUtils.toByteArray(new FileInputStream(filePath)));

引用文献:

最佳答案

您也可以测试此方法

String text = new String(Files.readAllBytes(Paths.get(path)));

还有带有直接缓冲区的 FileChannel

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

关于java - 在java IO中读取包含单行字符串的大文件的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979389/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com