您的位置:首页 >Java 中常见的文件读取方法有哪些?
发布于2023-04-25 阅读(0)
扫一扫,手机访问
代码:
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直接相加
@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及以上版本才可以使用。
@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左右。
代码:
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
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9