java文件IO性能的比较

    对于字符流和字节流,从字面上理解,字符(char)和字节(byte),所以在性能上改如果选择对应的流,如果你读取的是字符文件,最好选择带buffer缓存的字符流BufferedWriter,如果是二进制文件,最好选择带buffer的字节流BufferedOutputStream,从操作的简便性来看,JDK1.7的files最简单,如果对性能不是作特殊要求,选它最好。

   再啰嗦下句,haha,何时选择输入流,还是输出流,lz有自己的一个记忆方法,读入写出(从文件读入到内存中为输入流,写出到文件中或者网络即输出流)

package com.bky.hbhdc;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* 文件IO性能测试
*
* 1、如果对性能有要求就推荐使用带有缓存区的流来完成操作,如 BufferedWriter 或 BufferedOutputStream。
* 2、如果写入的内容是字符串的话,那么推荐使用 BufferedWriter,如果写入的内容是二进制文件的话就推荐使用 BufferedOutputStream。
*
* @author nanfengxiangbei
* @date 2020/12/29 10:25
*/
public class FileWriteReadTest {

public static void main(String[] args) throws Exception{
// 构建写入内容
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < 2000000; i++) {
stringBuilder.append("5141204341A01|2036974|1588329479|1588329486|aaaaaa_03,bbbbb_25");
}
// 写入内容
final String content = stringBuilder.toString();
// 存放文件的目录
final String filepath1 = "F:\besttv_data\test\write1.txt";
final String filepath2 = "F:\besttv_data\test\write2.txt";
final String filepath3 = "F:\besttv_data\test\write3.txt";
final String filepath4 = "F:\besttv_data\test\write4.txt";
final String filepath5 = "F:\besttv_data\test\write5.txt";
final String filepath6 = "F:\besttv_data\test\write6.txt";

// 方法一:使用 FileWriter 写文件
long stime1 = System.currentTimeMillis();
fileWriterTest(filepath1, content);
long etime1 = System.currentTimeMillis();
System.out.println("FileWriter 写入用时:" + (etime1 - stime1));

// 方法二:使用 BufferedWriter 写文件
long stime2 = System.currentTimeMillis();
bufferedWriterTest(filepath2, content);
long etime2 = System.currentTimeMillis();
System.out.println("BufferedWriter 写入用时:" + (etime2 - stime2));

// 方法三:使用 PrintWriter 写文件
long stime3 = System.currentTimeMillis();
printWriterTest(filepath3, content);
long etime3 = System.currentTimeMillis();
System.out.println("PrintWriterTest 写入用时:" + (etime3 - stime3));

// 方法四:使用 FileOutputStream 写文件
long stime4 = System.currentTimeMillis();
fileOutputStreamTest(filepath4, content);
long etime4 = System.currentTimeMillis();
System.out.println("FileOutputStream 写入用时:" + (etime4 - stime4));

// 方法五:使用 BufferedOutputStream 写文件
long stime5 = System.currentTimeMillis();
bufferedOutputStreamTest(filepath5, content);
long etime5 = System.currentTimeMillis();
System.out.println("BufferedOutputStream 写入用时:" + (etime5 - stime5));

// 方法六:使用 Files 写文件
long stime6 = System.currentTimeMillis();
filesTest(filepath6, content);
long etime6 = System.currentTimeMillis();
System.out.println("Files 写入用时:" + (etime6 - stime6));
}

/**
* 方法六:使用 Files 写文件
* @param filepath 文件目录
* @param content 待写入内容
* @throws IOException
*/
private static void filesTest(String filepath, String content) throws IOException {
Files.write(Paths.get(filepath), content.getBytes());
}

/**
* 方法五:使用 BufferedOutputStream 写文件
* @param filepath 文件目录
* @param content 待写入内容
* @throws IOException
*/
private static void bufferedOutputStreamTest(String filepath, String content) throws IOException {
try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
new FileOutputStream(filepath))) {
bufferedOutputStream.write(content.getBytes());
}
}

/**
* 方法四:使用 FileOutputStream 写文件
* @param filepath 文件目录
* @param content 待写入内容
* @throws IOException
*/
private static void fileOutputStreamTest(String filepath, String content) throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(filepath)) {
byte[] bytes = content.getBytes();
fileOutputStream.write(bytes);
}
}

/**
* 方法三:使用 PrintWriter 写文件
* @param filepath 文件目录
* @param content 待写入内容
* @throws IOException
*/
private static void printWriterTest(String filepath, String content) throws IOException {
try (PrintWriter printWriter = new PrintWriter(new FileWriter(filepath))) {
printWriter.print(content);
}
}

/**
* 方法二:使用 BufferedWriter 写文件
* @param filepath 文件目录
* @param content 待写入内容
* @throws IOException
*/
private static void bufferedWriterTest(String filepath, String content) throws IOException {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filepath))) {
bufferedWriter.write(content);
}
}

/**
* 方法一:使用 FileWriter 写文件
* @param filepath 文件目录
* @param content 待写入内容
* @throws IOException
*/
private static void fileWriterTest(String filepath, String content) throws IOException {
try (FileWriter fileWriter = new FileWriter(filepath)) {
fileWriter.append(content);
}
}
}

 

运行结果:

知人者智,自知者明,胜人者有力,自胜者强。
原文地址:https://www.cnblogs.com/nanfengxiangbei/p/14205708.html