转帖地址: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?(转帖)的更多相关文章

  1. java帮助文档下载

    JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和 ...

  2. JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载

    JAVA帮助文档全系列 JDK1.5 JDK1.6 JDK1.7 官方中英完整版下载JDK(Java Development Kit,Java开发包,Java开发工具)是一个写Java的applet和 ...

  3. java日期类型转换总结date timestamp calendar string

    用Timestamp来记录日期时间还是很方便的,但有时候显示的时候是不需要小数位后面的毫秒的,这样就需要在转换为String时重新定义格式.         Timestamp转化为String: S ...

  4. Java中Date各种相关用法

    Java中Date各种相关用法(一) 1.计算某一月份的最大天数 Java代码 Calendar time=Calendar.getInstance(); time.clear(); time.set ...

  5. java常用日期函数总结

    请记得要引入java.util.Date和java.text.SimpleDateFormat两个包 1.计算某一月份的最大天数 Calendar time=Calendar.getInstance( ...

  6. java做成windows服务,电子秤例子,开机自动启动

    使用Java Service Wrapper工具制作 1.windows32位下载地址 https://sourceforge.net/projects/wrapper/files/ 2.window ...

  7. JAVA的Date类与Calendar类(常用方法)

    http://blog.csdn.net/xiaopihai86/article/details/50827945 1.用Java.util.Calender来实现      Calendar cal ...

  8. java程序在windows系统作为服务程序运行

    Java程序很多情况下是作为服务程序运行的,在Un*x 平台下可以利用在命令后加“&”把程序作为后台服务运行,但在Windows下看作那个Console窗口在桌面上,你是否一直担心别的同时把你 ...

  9. java date总结

    Java 8 中 Date与LocalDateTime.LocalDate.LocalTime互转   Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant ...

随机推荐

  1. C++ 虚函数相关

    多态 C++的封装.继承和多态三大特性,封装没什么好说的,就是把事务属性和操作抽象成为类,在用类去实例化对象,从而对象可以使用操作/管理使用它的属性. 至于继承,和多态密不可分.基类可以进行派生,而派 ...

  2. 二叉树BinaryTree构建测试(无序)

    此测试仅用于二叉树基本的性质测试,不包含插入.删除测试(此类一般属于有序树基本操作). //二叉树树类 public class BinaryTree { public TreeNode root; ...

  3. pytorch之nn.Conv1d详解

    转自:https://blog.csdn.net/sunny_xsc1994/article/details/82969867,感谢分享 pytorch之nn.Conv1d详解

  4. httpd-2.4源码编译

    APR     APR(Apache portable Run-time libraries,Apache可移植运行库) 主要为上层的应用程序提供一个可以跨越多操作系统平台使用的底层支持接口库.在早 ...

  5. TightVNC安装

    软件版本:tightvnc-2.8.8-gpl-setup-64bit.msi 后面一路默认,虚拟机端口默认从5900开始递增.

  6. win10+CPU+Python3.6下安装pytorch

    - 写在前面 最近,学习之余,想继续搞以前的深度学习.虽然电脑上已经安装配置好tensorflow,但是鉴于其学习难度较高,且我是一个忠实的Pythoner(爱所有Python化的东西),因此果断入坑 ...

  7. 用js刷剑指offer(用两个栈实现队列)

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 牛客网链接 js代码 let stack1 = [] let stack2 = [] function ...

  8. 开启 clr enabled

    '; GO RECONFIGURE; GO '; GO RECONFIGURE; '; GO

  9. usb相关

    https://github.com/daynix/UsbDk/tree/master/UsbDk 更应该关注下libusb

  10. FFT/FWT

    最近舟游疯狂出货,心情很好~ FFT FWT 快速傅里叶变换(FFT) 具体的推导见这篇:胡小兔 - 小学生都能看懂的FFT!!! (写的很好,不过本小学生第一次没看懂0.0) 总结下关键内容 ~ P ...