多种方式实现java写文件

多种方式实现java写文件

本文介绍java如何写输入流至文件,首先介绍纯java实现,然后是guava Files实现,最后介绍Apache Commons IO 库,当然还有其他,如之前文章中提到的Spring StreamUtils

java实现写文件

开始java的实现方式:

@Test
public void whenConvertingToFile_thenCorrect() 
  throws IOException {

    InputStream initialStream = new FileInputStream(
      new File("src/main/resources/sample.txt"));
    byte[] buffer = new byte[initialStream.available()];
    initialStream.read(buffer);

    File targetFile = new File("src/main/resources/targetFile.tmp");
    OutputStream outStream = new FileOutputStream(targetFile);
    outStream.write(buffer);
}

上面示例需要指出,输入流是已知的磁盘文件或内存流,因此无需检查大小,只要内存允许,简单读取并一次性写入即可。

如果输入流与持续的数据流关联,比如当前活动连接的http响应,那么一次性读取整个流是不可取的,这时我们需要确保持续读取,直到流的结尾。

@Test
public void whenConvertingInProgressToFile_thenCorrect() 
  throws IOException {

    InputStream initialStream = new FileInputStream(
      new File("src/main/resources/sample.txt"));
    File targetFile = new File("src/main/resources/targetFile.tmp");
    OutputStream outStream = new FileOutputStream(targetFile);

    byte[] buffer = new byte[8 * 1024];
    int bytesRead;
    while ((bytesRead = initialStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
    }
    IOUtils.closeQuietly(initialStream);
    IOUtils.closeQuietly(outStream);
}

IOUtils是Commons IO 提供的工具类,用于关闭流。Guava中也有Closer类实现。

java8实现也很简洁:

@Test
public void whenConvertingAnInProgressInputStreamToFile_thenCorrect2() 
  throws IOException {

    InputStream initialStream = new FileInputStream(
      new File("src/main/resources/sample.txt"));
    File targetFile = new File("src/main/resources/targetFile.tmp");

    java.nio.file.Files.copy(
      initialStream, 
      targetFile.toPath(), 
      StandardCopyOption.REPLACE_EXISTING);

    IOUtils.closeQuietly(initialStream);
}

guava实现写文件

guava实现很简单,并且无需关闭。

@Test
public void whenConvertingInputStreamToFile_thenCorrect3() 
  throws IOException {

    InputStream initialStream = new FileInputStream(
      new File("src/main/resources/sample.txt"));
    byte[] buffer = new byte[initialStream.available()];
    initialStream.read(buffer);

    File targetFile = new File("src/main/resources/targetFile.tmp");
    Files.write(buffer, targetFile);
}

Commons IO实现

commons io 实现代码更少。

@Test
public void whenConvertingInputStreamToFile_thenCorrect4() 
  throws IOException {
    InputStream initialStream = FileUtils.openInputStream
      (new File("src/main/resources/sample.txt"));

    File targetFile = new File("src/main/resources/targetFile.tmp");

    FileUtils.copyInputStreamToFile(initialStream, targetFile);
}

总结

本文介绍了三种java写输入流至文件的方式,形式不重要,重要的是理解其中原理,避免读取效率低且代码冗长。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值