003-Spring 中的StreamUtils
一、概述
StreamUtils是spring中用于处理流的类,是java.io包中inputStream和outputStream,不是java8中Steam。使用时仅依赖spring-core
二、使用
基本的输入流读取成字符串
public static String getStringFromInputStream(InputStream input) throws IOException {
StringWriter writer = new StringWriter();
IOUtils.copy(input, writer, "UTF-8");
return writer.toString();
}
2.1、拷贝inputStream内容至outputStream:StreamUtils.copy(in, out);
两个参数,第一个为输入,第二个为拷贝至的
@Test
public void whenCopyInputStreamToOutputStream_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
String outputFileName = "src/test/resources/output.txt";
File outputFile = new File(outputFileName);
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFile); StreamUtils.copy(in, out); assertTrue(outputFile.exists());
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(inputFileContent, outputFileContent);
}
2.2、拷贝inputStream部分内容至outputStream,使用copyRange()方法拷贝一定范围的内容:
copyRange方法有四个参数,inputStream,outputStream,开始拷贝位置,结束拷贝位置。如果我们指定的长度超过inputStream的长度呢?copyRange方法仅拷贝至流的结尾。
@Test
public void whenCopyRangeOfInputStreamToOutputStream_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
String outputFileName = "src/test/resources/output.txt";
File outputFile = new File(outputFileName);
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName); StreamUtils.copyRange(in, out, 1, 10); assertTrue(outputFile.exists());
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName)); assertEquals(inputFileContent.substring(1, 11), outputFileContent);
}
2.3、拷贝字符串至outputStream
copy方法带三个参数:被拷贝的字符串,写文件时指定的字符集,指定目的地(outputStream)
@Test
public void whenCopyStringToOutputStream_thenCorrect() throws IOException {
String string = "Should be copied to OutputStream.";
String outputFileName = "src/test/resources/output2.txt";
File outputFile = new File(outputFileName);
OutputStream out = new FileOutputStream(outputFileName); StreamUtils.copy(string, StandardCharsets.UTF_8, out); assertTrue(outputFile.exists());
String outputFileContent = getStringFromInputStream(new FileInputStream(outputFileName));
assertEquals(outputFileContent, string);
}
2.4、将inputStream内容拷贝为字符串
@Test
public void whenCopyInputStreamToString_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
InputStream is = new FileInputStream(inputFileName);
String content = StreamUtils.copyToString(is, StandardCharsets.UTF_8); String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName));
System.out.println(content);
assertEquals(inputFileContent, content);
}
2.5、拷贝inputStream内容至字节数组
@Test
public void whenCopyInputStreamToByteArray_thenCorrect() throws IOException {
String inputFileName = "src/test/resources/input.txt";
InputStream is = new FileInputStream(inputFileName);
byte[] out = StreamUtils.copyToByteArray(is); String content = new String(out);
String inputFileContent = getStringFromInputStream(new FileInputStream(inputFileName)); assertEquals(inputFileContent, content);
}
2.6、删除流中所有剩余数据
StreamUtils.drain(in);
2.7、获得一个有效空输入流
public InputStream getInputStream() {
return StreamUtils.emptyInput();
}
2.8、两个重载方法nonClosing(),inputStream和outputStream流可以作为参数,用于返回无需关闭的inputStream和outputStream流
public InputStream getNonClosingInputStream() throws IOException {
InputStream in = new FileInputStream("src/test/resources/input.txt");
return StreamUtils.nonClosing(in);
}
003-Spring 中的StreamUtils的更多相关文章
- Spring 中StreamUtils教程
本文我们介绍StreamUtils类使用.StreamUtils是spring中用于处理流的类,是java.io包中inputStream和outputStream,不是java8中Steam.使用时 ...
- Velocity初探小结--Velocity在spring中的配置和使用
最近正在做的项目前端使用了Velocity进行View层的数据渲染,之前没有接触过,草草过了一遍,就上手开始写,现在又回头细致的看了一遍,做个笔记. velocity是一种基于java的模板引擎技术, ...
- Spring中Bean的作用域、生命周期
Bean的作用域.生命周期 Bean的作用域 Spring 3中为Bean定义了5中作用域,分别为singleton(单例).protot ...
- Spring中Bean的实例化
Spring中Bean的实例化 在介绍Bean的三种实例化的方式之前,我们首先需要介绍一下什么是Bean,以及Bean的配置方式. 如果 ...
- 模拟实现Spring中的注解装配
本文原创,地址为http://www.cnblogs.com/fengzheng/p/5037359.html 在Spring中,XML文件中的bean配置是实现Spring IOC的核心配置文件,在 ...
- Spring中常见的bean创建异常
Spring中常见的bean创建异常 1. 概述 本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...
- Spring中配置数据源的4种形式
不管采用何种持久化技术,都需要定义数据源.Spring中提供了4种不同形式的数据源配置方式: spring自带的数据源(DriverManagerDataSource),DBCP数据源,C3P0数据源 ...
- spring中InitializingBean接口使用理解
InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候会执行该方法. 测试程序如下: imp ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
随机推荐
- Hive学习 系列博客
原 Hive作业优化 原 Hive学习六:HIVE日志分析(用户画像) 原 Hive学习五--日志案例分析 原 Hive学习三 原 Hive学习二 原 Hive学习一 博客来源,https://blo ...
- 前端模块化和AMD、CMD规范
前端模块化和AMD.CMD规范 先看下基础:https://github.com/seajs/seajs/issues/547
- 迁移TFS 2012服务至新的电脑硬件
迁移TFS 2012的时候碰到一些问题, 中文记录很少, 英文的记录也比较零散. 这里记录最直接和简单的方法. 环境: 1. 公司域环境, 所有TFS用户都是公司域帐户. 2. TFS从一台服务器转移 ...
- html5_websocket_tomcat8
目录 前言 后端 浏览器前端 java的client 注意 前言 HTML5 WebSocket实现了服务器与浏览器的双向通讯,开销小,实时性高,常用于即时通讯和对信息实时性要求比较高的应用. 下面讲 ...
- Ionic学习笔记3_ionic指令简单布局
1) 添加引用类库(ionic样式和ionic js文件) 2) 标题栏,页脚栏,内容区 3) Js引入ionic类库,添加页面操作方法和对象 4) 数据初始化 5) Html页面 ...
- SVN各种错误提示产生原因及处理方法大全(转)
SVN各种错误提示产生原因及处理方法大全 1. svn: Server sent unexpected return value (500 Internal Server Error) in resp ...
- 查看文件内容- 删除某个运行程序的所有进程-nohup后台执行程序
1 查看文件内容: tail -f test.txt 2 查看端口 netstat nlp 3 删除某个运行程序的所有进程 ps -ef|grep translateService.py|grep - ...
- PHP 7的一些引人注目的新特性简单介绍
1. ?? 运算符(NULL 合并运算符)把这个放在第一个说是因为我觉得它很有用.用法: ? 1 $a = $_GET['a'] ?? 1; 它相当于: ? 1 2 <?php $a = iss ...
- ZOJ 3962 Seven Segment Display 16进制的八位数加n。求加的过程中所有的花费。显示[0,F]有相应花费。
Seven Segment Display Time Limit: Seconds Memory Limit: KB A seven segment display, or seven segment ...
- python之filter()函数
filter()函数是python内置的一个高阶函数. filter()函数接受一个函数f 和一个list,这个函数f的作用是对每个元素进行判断,返回True或False,filter()根据判断结果 ...