今天小编给大家分享一下Java文件读取的方法有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
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
这里将文件读取出来之后存储方式改下,每次创建新的String字符串,测试一下每次创建新的字符串和使用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,可见多次创建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