【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 ...
随机推荐
- vim调试Shell脚本: unexpected EOF while looking for matching
往往在编写脚本完后测试,出现错误需要调试,vim 是一种强大的文本编辑器,对调试也很有帮助.如果指定用不同的颜色显示某些错误,通过配置 .vimrc 文件就会替您完成大部分调试工作. 小柏在测试脚本时 ...
- java - day010 - 基本类型包装,自动装箱和拆箱,日期,集合
基本类型的包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolea ...
- linux(3)
一.用户和组的管理 Linux/Unix是多用户系统: root是超级用户,拥有最高权限.其它用户及权限由root来管理.对比Windows系统: 控制面板 -> 管理工具 -> 计算机管 ...
- Paper Reading:HyperNet
论文:HyperNet: Towards Accurate Region Proposal Generation and Joint Object Detection 发表时间:2016 发表作者:( ...
- Java字节码常量池深度剖析与字节码整体结构分解
常量池深度剖析: 在上一次[https://www.cnblogs.com/webor2006/p/9416831.html]中已经将常量池分析到了2/3了,接着把剩下的分析完,先回顾一下我们编译的源 ...
- GC详解及Minor GC和Full GC触发条件
GC,即就是Java垃圾回收机制.目前主流的JVM(HotSpot)采用的是分代收集算法.与C++不同的是,Java采用的是类似于树形结构的可达性分析法来判断对象是否还存在引用.即:从gcroot开始 ...
- 使用@ConfigurationProperties注解 提示 “Spring Boot Configuration Annotation Processor not found in classpath ”
解决方案: 在 pom.xml 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...
- maven 安装第三方jar到本地 出现 The goal you specified requires a project to execute but there is no POM in this directory 错误
原因是因为操作系统的差异导致,把所有参数加上引号即可. 如下所示: mvn install:install-file "-Dfile=cobra.jar" "-Dgrou ...
- Python和Shell交互工具 ShellPy
ShellPy 是一款Python和Shell的交互工具.一般来说,我们会通过Subprocess.Popen或者Command模块执行一条Shell命令或脚本,然后通过返回的标准输出和错误输出来得到 ...
- 洛谷P1169 棋盘制作【悬线法】【区间dp】
题目:https://www.luogu.org/problemnew/show/P1169 题意:n*m的黑白格子,找到面积最大的黑白相间的正方形和矩形. 思路:传说中的悬线法!用下面这张图说明一下 ...