• 文件处于磁盘上或者流处于内存中
在输入流有已知的和预处理的数据时,如在硬盘上的文件或者在流处于内存中。这种情况下,不需要做边界校验,并且内存容量条件允许的话,可以简单的读取并一次写入。
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); 
 
基于Guava的实现
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的实现
InputStream initialStream = FileUtils.openInputStream(new File("src/main/resources/sample.txt"));
File targetFile = new File("src/main/resources/targetFile.tmp");
FileUtils.copyInputStreamToFile(initialStream, targetFile);
  • 输入流映射正在进行的数据流
如果输入流链接到正在进行的数据流上,如来自正在进行的链接的HTTP响应,此时可能无法一次读取整个流。这种情况下,我们需要确保一直读取到流的尽头。
File targetFile = new File("src/main/resources/targetFile.tmp");
try(InputStream initialStream = new FileInputStream(new File("src/main/resources/sample.txt"));
OutputStream outStream = new FileOutputStream(targetFile)) {
byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = initialStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
}
另一种实现方式:
try (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);
} catch (Exception e) {
e.printStackTrace();
}

Java InputStream转File的更多相关文章

  1. Java InputStream、String、File相互转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  2. Java InputStream、String、File相互转化 --- good

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  3. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

  4. 云笔记项目- 上传文件报错"java.lang.IllegalStateException: File has been moved - cannot be read again"

    在做文件上传时,当写入上传的文件到文件时,会报错“java.lang.IllegalStateException: File has been moved - cannot be read again ...

  5. inputStream、File、Byte、String等等之间的相互转换

    一:inputStream转换 1.inputStream转为byte //方法一 org.apache.commons.io.IOUtils包下的实现(建议) IOUtils.toByteArray ...

  6. com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\salary-card\target\salary-card-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\keystore\login_id_rsa 资源未找到

    com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\sala ...

  7. Java-Runoob:Java Stream、File、IO

    ylbtech-Java-Runoob:Java Stream.File.IO 1.返回顶部 1. Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出 ...

  8. java中的File文件读写操作

    之前有好几次碰到文件操作方面的问题,大都由于时间太赶而没有好好花时间去细致的研究研究.每次都是在百度或者博客或者论坛里面參照着大牛们写的步骤照搬过来,之后再次碰到又忘记了.刚好今天比較清闲.于是就在网 ...

  9. Java学习:File类

    Java学习:File类 File类的概述 重点:记住这三个单词 绝对路径和相对路径 File类的构造方法 File类判断功能的方法 File类创建删除功能的方法 File类获取(文件夹)目录和文件夹 ...

随机推荐

  1. 成都Uber优步司机奖励政策(1月15日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. LeetCode: 58. Length of Last Word(Easy)

    1. 原题链接 https://leetcode.com/problems/length-of-last-word/description/ 2. 题目要求 给定一个String类型的字符串,字符串中 ...

  3. Machine Learning Basic Knowledge

    常用的数据挖掘&机器学习知识(点) Basis(基础): MSE(MeanSquare Error 均方误差),LMS(Least MeanSquare 最小均方),LSM(Least Squ ...

  4. 在ubuntu trusty下安装python的rasterio库

    就这些吧.. apt-get update -y apt-get install -y software-properties-common add-apt-repository ppa:ubuntu ...

  5. (转载)Unity3d中的属性(Attributes)整理

    附加: float字段检视面板修改:[Range(1,10)] 对属性进行分组:[Header("xxx")] 工具栏中调用方法,类文件需在Editor文件夹中:[MenuIte( ...

  6. 【paging_Class 分页类】使用说明

    类名:paging_Class 说明:分页类 注意: 1) 支持百万级数据分页 2) 支持多种类型的SQL语法,比如 Left Join 等. 3) 自动保存查询中的错误情况,记录保存在:/Cache ...

  7. 245. Subtree【LintCode java】

    Description You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds ...

  8. python中的迭代器与生成器

    迭代器 迭代器的引入 假如我现在有一个列表l=['a','b','c','d','e'],我想取列表中的内容,那么有几种方式? 1.通过索引取值 ,如了l[0],l[1] 2.通过for循环取值 fo ...

  9. 换抵挡装置 (Kickdown,ACM/ICPC NEERC 2006,UVa1588

    题目描述:算法竞赛入门经典习题3-11 题目思路:1.两长条移动匹配 2.上下调换,取小者 #include <stdio.h> #include <string.h> int ...

  10. appium启动APP配置参数:

    一.Android启动app   python启动脚本如下:   from appium import webdriver   desired_caps = {} desired_caps['plat ...