转帖地址: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. Django基础目录

    Django基础(一):基础引用 Django基础(二):环境配置 Django—model系统:ORM基础 Django—model系统:ORM字段和参数 Django—model系统:ORM对数据 ...

  2. linux 基础8-shell script

    1. 什么是shell script 1.1 介绍: 什么是 shell script (程序化脚本) 呢?就字面上的意义,我们将他分为两部份. 在『 shell 』部分,我们在bash当中已经提过了 ...

  3. 小白学Python | 最简单的Django 简明教程

    作者:浅雨凉 来源:http://www.cnblogs.com/qianyuliang/p/6814376.html 一.Django简介 1. web框架介绍 具体介绍Django之前,必须先介绍 ...

  4. Vue入门——v-if和v-show

    v-if 特点:每次都会重新删除或创元素 有较高的切换性能消耗 v-show 特点:每次不会重新进行DOM的删除和创建操作,只是切换了元素的display:none样式 有较高的初始渲染消耗

  5. DataSet,DataTable,DataView、DataRelation

    一.创建Dataset和DataTable DataSet ds = new DataSet();//DataSetName默认为"NewDataSet" DataTable ta ...

  6. Checklist的补充规则

    常见问题: 1.什么时候该补充Checklist? 2.Checklist应该写哪些用例? 3.自己写的模块是否自己执行? 4.什么时候执行Checklist? 5.执行完Checklist大概需要花 ...

  7. Python操作MySQL数据库,插入重复数据

    sql = "INSERT  INTO test_c(id,name,sex)values(%s,%s,%s)" param = (1,'AJ','MAN') n = cursor ...

  8. 003_软件安装之_Visual Studio 2012

    Visual Studio 2012安装,里面有视频教程,还有秘钥,连接失效联系我 2012版: 链接:https://pan.baidu.com/s/1BRE46cTKJW58YZ3lBFyjMw ...

  9. 参数类型 (@Controller层)

    @RequestMapping(path = "/listPage")@SuppressWarnings("unchecked")@BussinessLog(v ...

  10. Poj 2976 Dropping tests(01分数规划 牛顿迭代)

    Dropping tests Time Limit: 1000MS Memory Limit: 65536K Description In a certain course, you take n t ...