【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 ...
随机推荐
- sql查询并把数据更新到另一个表中
update OpenBills set peopleCount=(select rtNumber from Rooms where obId='ZD201005223') where obId='Z ...
- JAVA面试核心教程|Java面试基础知识点总结
Java中的原始数据类型都有哪些,它们的大小及对应的封装类是什么? byte——1 byte——Byte short——2 bytes——Short int——4 bytes——Integer lon ...
- rsync & sersync 实时同步
1.根据之前一篇关于rsync的随笔部署好rsync服务后,可以开始inotify的部署 2.sersync的部署 ①.部署服务(安装和配置过程) #Master 部署Sersync服务 mkdir ...
- PHP获取不到url传递参数中#&等特殊字符解决方法
有些符号在URL中是不能直接传递的,无法传入PHP处理,比如#&等符号,通过$_GET是获取不到的,比如一个域名https://localhost/url.php?url=yangyufei+ ...
- es批量导入进一对多的数据
es批量导入进一对多的数据 我有一个产品表 一个产品对应多个属性名 一个属性名对应多个属性值 一个产品还对应一个分类名称 控制层 @ApiOperation(value = "导入所有 ...
- 2020 WPF开发革命性时代,DevExpress为你护航
下载DevExpress v19.2完整版 通过DevExpress WPF Controls,您能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸 ...
- 洛谷P1144 最短路计数【堆优化dijkstra】
题目:https://www.luogu.org/problemnew/show/P1144 题意:问1到各个节点的最短路有多少条. 思路:如果松弛的时候发现是相等的,说明可以经过该点的最短路径到达当 ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) C. p-binary
链接: https://codeforces.com/contest/1247/problem/C 题意: Vasya will fancy any number as long as it is a ...
- [Google Guava] 1.3-常见Object方法
原文链接 译者: 沈义扬 equals 当一个对象中的字段可以为null时,实现Object.equals方法会很痛苦,因为不得不分别对它们进行null检查.使用Objects.equal帮助你执行n ...
- winfrom窗体自适应
using System.Runtime.InteropServices; public class Win32 { public const Int32 AW_HOR_POSITIVE = 0x00 ...