【English】What is a Java StringWriter, and when should I use it?(转帖)
转帖地址:http://www.it1352.com/989366.html
Question:
What is a Java StringWriter, and when should I use it?
I have read the documentation and looked here, but I do not understand when I should use it.
Answer:
It is a specialized Writer that writes characters to a StringBuffer, and then we use method like toString() to get the string result.
When StringWriter is used is that you want to write to a string, but the API is expecting a Writer or a Stream. It is a compromised, you use StringWriter only when you have to, since StringBuffer/StringBuilder to write characters is much more natural and easier,which should be your first choice.
Here is two of a typical good case to use StringWriter
1.Converts the stack trace into String, so that we can log it easily.
StringWriter sw = new StringWriter();//create a StringWriter PrintWriter pw = new PrintWriter(sw);//create a PrintWriter using this string writer instance t.printStackTrace(pw);//print the stack trace to the print writer(it wraps the string writer sw) String s=sw.toString(); // we can now have the stack trace as a string
2.Another case will be when we need to copy from an InputStream to chars on a Writer so that we can get String later, using Apache commons IOUtils#copy :
StringWriter writer = new StringWriter(); IOUtils.copy(inputStream, writer, encoding);//copy the stream into the StringWriter String result = writer.toString();
--END-- 2019年11月3日11:06:45
【English】What is a Java StringWriter, and when should I use it?(转帖)的更多相关文章
- java帮助文档下载
JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和 ...
- JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载
JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和 ...
- java日期类型转换总结date timestamp calendar string
用Timestamp来记录日期时间还是很方便的,但有时候显示的时候是不需要小数位后面的毫秒的,这样就需要在转换为String时重新定义格式. Timestamp转化为String: S ...
- Java中Date各种相关用法
Java中Date各种相关用法(一) 1.计算某一月份的最大天数 Java代码 Calendar time=Calendar.getInstance(); time.clear(); time.set ...
- java常用日期函数总结
请记得要引入java.util.Date和java.text.SimpleDateFormat两个包 1.计算某一月份的最大天数 Calendar time=Calendar.getInstance( ...
- java做成windows服务,电子秤例子,开机自动启动
使用Java Service Wrapper工具制作 1.windows32位下载地址 https://sourceforge.net/projects/wrapper/files/ 2.window ...
- JAVA的Date类与Calendar类(常用方法)
http://blog.csdn.net/xiaopihai86/article/details/50827945 1.用Java.util.Calender来实现 Calendar cal ...
- java程序在windows系统作为服务程序运行
Java程序很多情况下是作为服务程序运行的,在Un*x 平台下可以利用在命令后加“&”把程序作为后台服务运行,但在Windows下看作那个Console窗口在桌面上,你是否一直担心别的同时把你 ...
- java date总结
Java 8 中 Date与LocalDateTime.LocalDate.LocalTime互转 Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant ...
随机推荐
- 如何将Chrome本地安装的扩展应用导出到本地
有时由于种种原因,我们不能直接使用Chrome web store进行Chrome扩展应用的安装,这时可以让一位已经安装了某Chrome扩展应用的朋友将他的应用导出到本地成为.crx文件,然后发给你, ...
- insert buffer/change buffer double write buffer,双写 adaptive hash index(AHI) innodb的crash recovery innodb重要参数 innodb监控
https://yq.aliyun.com/articles/41000 http://blog.itpub.net/22664653/viewspace-1163838/ http://www.cn ...
- Linux用户管理——useradd
除了useradd还有一个命令adduser,两者是链接关系 [root@51cto ~]# which adduser /usr/sbin/adduser [root@51cto ~]# which ...
- 关于zsh在使用scp时报错zsh: no matches found: scp
root@banxia:scp root@172.16.13.150:/123/* . zsh: no matches found: root@172.16.13.150:/123/* root@ba ...
- 用js刷剑指offer(调整数组顺序使奇数位于偶数前面)
题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 牛客网链接 js代码 ...
- 通用分页model封装pageList
package selfimpr.page; import java.util.List; /** * 分页模型 * @param <T> 数据泛型 * @author selfimpr ...
- css选择器学习(一)
1.通用选择器“*”和元素选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- linux实操_硬盘
1.硬盘分区 硬盘说明: 查看分区和挂载情况 语法: lsblk -f lsblk 2.增加硬盘 (1)虚拟机添加硬盘 (2)分区 fdisk /dev/sdb (3)格式化 mkfs -text4 ...
- Python+request 分模块存放接口,多接口共用参数URL、headers的抽离,添加日志打印等《三》
主要介绍内容如下: 1.分模块存放接口 2.多接口共用参数URL.headers的抽离为配置文件 3.添加日志打印 4.一个py文件运行所有所测的接口 如上介绍内容的作用: 1.分模块存放接口:方便多 ...
- Ubuntu Linux使用sudo命令搭建java环境
搬运stackoverflow 注意,以下所有命令需要在root权限下执行 1. 在Ubuntu下打开终端命令或用ssh连接到linux. 2. 更新仓库(只有Ubuntu17.4及以下系统可用): ...