博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读写文件效率测试
阅读量:4182 次
发布时间:2019-05-26

本文共 3985 字,大约阅读时间需要 13 分钟。

写文件

package com.xiaobu.note.daily.autoCloseAble;import lombok.SneakyThrows;import org.springframework.util.StopWatch;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/7/31 17:33 * @description */public class WriteFile {
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch(); stopWatch.start("FileOutputStream 写文件"); writeByFileOutStream(); stopWatch.stop(); stopWatch.start("BufferedOutputStream 写文件"); writeByBufferedOutputStream(); stopWatch.stop(); stopWatch.start("FileWriter 写文件"); writeByFileWriter(); stopWatch.stop(); System.out.println("stopWatch.prettyPrint() = " + stopWatch.prettyPrint()); } @SneakyThrows public static void writeByFileOutStream() {
FileOutputStream out = null; int count = 1000;//写文件行数 out = new FileOutputStream(new File("D:\\data1.txt")); for (int i = 0; i < count; i++) {
out.write("测试java 文件操作\r\n".getBytes()); } } @SneakyThrows public static void writeByBufferedOutputStream() {
FileOutputStream outSTr = new FileOutputStream(new File("D:\\data2.txt")); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outSTr); int count = 1000;//写文件行数 for (int i = 0; i < count; i++) {
bufferedOutputStream.write("测试java 文件操作\r\n".getBytes()); } } @SneakyThrows public static void writeByFileWriter() {
FileWriter fw = new FileWriter("D:\\data3.txt"); int count = 1000;//写文件行数 for (int i = 0; i < count; i++) {
fw.write("测试java 文件操作\r\n"); } }}

1579052262(1).jpg

可以看出写文件最快的是BufferedOutputStream

读文件

package com.xiaobu.note.daily.autoCloseAble;import lombok.SneakyThrows;import org.springframework.util.StopWatch;import java.io.*;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2019/7/31 17:33 * @description */public class ReadFile {
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch(); stopWatch.start("开始执行 readByFileInputStream 方法"); readByFileInputStream(); stopWatch.stop(); stopWatch.start("开始执行 readByBuffReader 方法"); readByBuffReader(); stopWatch.stop(); stopWatch.start("开始执行 readByFileReader 方法"); readByFileReader(); stopWatch.stop(); System.out.println("stopWatch.prettyPrint() = " + stopWatch.prettyPrint()); } @SneakyThrows public static void readByFileInputStream(){
File file = new File("D:/data1.txt"); try (FileInputStream fileInputStream = new FileInputStream(file); //FileOutputStream fileOutputStream = new FileOutputStream(file) ) {
byte[] buf = new byte[1024]; int length=0; String result = null; while ((length=fileInputStream.read(buf))!=-1){
result = new String(buf,0,length); } System.out.println("result = " + result);; } } @SneakyThrows public static void readByFileReader(){
File file = new File("D:/data2.txt"); FileReader fileReader = new FileReader(file); char[] buf = new char[1024]; int length=0; String result=null; while ((length=fileReader.read(buf))!=-1){
result = new String(buf,0,length); } System.out.println("result = " + result); } @SneakyThrows public static void readByBuffReader(){
File file = new File("D:/data3.txt"); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String result=null; while ((result=bufferedReader.readLine())!=null){
System.out.println("result = " + result); } }}

1579052705(1).jpg

可以看出读效率最快的是FileReader

转载地址:http://vwgai.baihongyu.com/

你可能感兴趣的文章
linux文件权限和访问模式
查看>>
vi编辑器
查看>>
linux文件管理
查看>>
程序设计语言
查看>>
认识bash 这个Shell
查看>>
变量+shell变量(1)
查看>>
Shell 替换+运算符
查看>>
Shell注释+字符串+数组
查看>>
难得的小日记
查看>>
What is DNS?
查看>>
What is URL?
查看>>
从输入网址到显示网页的全过程分析
查看>>
What is hosts?
查看>>
一张图读懂互联网大咖
查看>>
grep命令+正则表达式
查看>>
here文档
查看>>
linux 两个装逼的命令
查看>>
C语言中的static
查看>>
open系统调用
查看>>
man 命令
查看>>