淺談Java幾種文件讀取方式耗時
項目中經(jīng)常會遇到文件讀寫,不同的讀寫方式速度之間有多大差異呢?
這里自己沒有使用外部的依賴庫,使用Java原生的文件讀寫方法:
測試文件大小,7.1M
BufferedReader
代碼:
public static String ReadFileByBufferReaderToString(String path) {
if (TextUtils.isEmpty(path)) {
return "";
}
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
String tempStr;
while ((tempStr = bufferedReader.readLine()) != null) {
stringBuilder.append(tempStr).append(System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
這里我們使用stringbuilder去存儲讀取出來的字符串,加日志查看耗時,讀取一個
onClick: readFileByBufferReaderStringBuilder tims use is 86
這里將文件讀取出來之后存儲方式改下,每次創(chuàng)建新的String字符串,測試一下每次創(chuàng)建新的字符串和使用StringBuilder之間的性能差異:
public static String ReadFileByBufferReaderToStringUseString(String path) {
if (TextUtils.isEmpty(path)) {
return "";
}
String result = "";
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
String tempStr;
while ((tempStr = bufferedReader.readLine()) != null) {
result += tempStr;
}
} catch (IOException e) {
e.printStackTrace();
}
Log.i(TAG, "ReadFileToString: read success ");
return result;
}2023-04-08 23:06:06.141 18416-18518/com.example.androidstart I/TestFileReadSpeed: onClick: readFileByBufferReaderString tims use is 264041
花了264041 ms,可見多次創(chuàng)建String對象對性能消耗非常大,所以字符串拼接的時候一定要使用StringBuilder,不能使用String直接相加
Files.readAllBytes
@RequiresApi(api = Build.VERSION_CODES.O)
public static String ReadFileByReadAllBytesReaderToString(String path) {
if (TextUtils.isEmpty(path)) {
return "";
}
String result = null;
try {
result = new String(Files.readAllBytes(Paths.get(path)));
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
2023-04-09 17:38:06.989 7078-7359/com.example.androidstart I/TestFileReadSpeed: onClick: ReadFileByReadAllBytesReaderToString tims use is 68
耗時68ms,比上面的BufferReader行一行讀取會快一些,但是這個API有一些限制就是必須在AndroidO及以上版本才可以使用。
Files.lines
@RequiresApi(api = Build.VERSION_CODES.O)
public static String ReadFileByByFilesReadLinesToString(String path) {
if(TextUtils.isEmpty(path)){
return "";
}
StringBuilder stringBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(path))) {
stream.forEach(new Consumer<String>() {
@Override
public void accept(String s) {
stringBuilder.append(s);
}
});
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}2023-04-09 17:46:14.342 7078-7078/com.example.androidstart I/TestFileReadSpeed: onClick: ReadFileByByFilesReadLinesToString tims use is 102
Files.lines耗時中等在100ms左右。
CommonIO::readFileToString
代碼:
public static String ReadFileByCommonIOReadFileToString(String path) {
if (TextUtils.isEmpty(path)) {
return "";
}
try {
return FileUtils.readFileToString(new File(path), Charset.defaultCharset());
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
2023-04-09 17:53:34.204 8292-8292/com.example.androidstart I/TestFileReadSpeed: onClick: ReadFileByCommonIOReadFileToString tims use is 70
耗時為70ms
綜上:(Files.readAllBytes 和 FileUtils.readFileToString耗時想近) 優(yōu)于 (BufferReader和Files.lines耗時相近)
到此這篇關(guān)于淺談Java幾種文件讀取方式耗時的文章就介紹到這了,更多相關(guān)Java 文件讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot application properties配置實例代碼詳解
本文通過代碼給大家介紹了spring boot application properties配置方法,需要的的朋友參考下吧2017-07-07
SpringBoot2.x 整合Spring-Session實現(xiàn)Session共享功能
這篇文章主要介紹了SpringBoot2.x 整合Spring-Session實現(xiàn)Session共享功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
java圖片滑動驗證(登錄驗證)原理與實現(xiàn)方法詳解
這篇文章主要介紹了java圖片滑動驗證(登錄驗證)原理與實現(xiàn)方法,結(jié)合實例形式詳細(xì)分析了java圖片滑動登錄驗證的相關(guān)原理、實現(xiàn)方法與操作技巧,需要的朋友可以參考下2019-09-09
靈活控制任務(wù)執(zhí)行時間的Cron表達(dá)式范例
這篇文章主要為大家介紹了靈活控制任務(wù)執(zhí)行時間的Cron表達(dá)式范例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10

