IOUtils的使用
来源:互联网 发布:pscc2018破解软件 编辑:程序博客网 时间:2024/05/22 13:01
[转载于http://uule.iteye.com/blog/2107010](http://uule.iteye.com/blog/2107010)
1、IOUtils.toString(InputStream input)
- protected boolean writeEJBJar(String path, WebServiceProject project){
- String ejbJar = null;
- try {
- ejbJar = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(“META-INF/ws/ejb-jar.comm.template”));
- logger.debug(”get template pass…”);
- ejbJar = String.format(ejbJar, project.getName());
- logger.debug(”template format :\n” + ejbJar);
- FileUtils.writeStringToFile(new File(path), ejbJar, “UTF-8”);
- logger.debug(”write ” + path + “ to harddrive pass…”);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error(”writeBuild error : ” + e.getMessage());
- return false;
- }
- return true;
- }
protected boolean writeEJBJar(String path, WebServiceProject project){ String ejbJar = null; try { ejbJar = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("META-INF/ws/ejb-jar.comm.template")); logger.debug("get template pass..."); ejbJar = String.format(ejbJar, project.getName()); logger.debug("template format :\n" + ejbJar); FileUtils.writeStringToFile(new File(path), ejbJar, "UTF-8"); logger.debug("write " + path + " to harddrive pass..."); } catch (Exception e) { e.printStackTrace(); logger.error("writeBuild error : " + e.getMessage()); return false; } return true; }
源码:
- //注意此处用的StringWriter来实现字符输出展示
- public static String toString(InputStream input) throws IOException {
- StringWriter sw = new StringWriter();
- copy(input, sw);
- return sw.toString();
- }
//注意此处用的StringWriter来实现字符输出展示public static String toString(InputStream input) throws IOException { StringWriter sw = new StringWriter(); copy(input, sw); return sw.toString(); }
- public static void copy(InputStream input, Writer output)
- throws IOException {
- InputStreamReader in = new InputStreamReader(input);
- copy(in, output);
- }
- public static int copy(Reader input, Writer output) throws IOException {
- long count = copyLarge(input, output);
- if (count > Integer.MAX_VALUE) {
- return -1;
- }
- return (int) count;
- }
public static void copy(InputStream input, Writer output) throws IOException { InputStreamReader in = new InputStreamReader(input); copy(in, output); } public static int copy(Reader input, Writer output) throws IOException { long count = copyLarge(input, output); if (count > Integer.MAX_VALUE) { return -1; } return (int) count; }
- /**
- * The default buffer size to use.
- */
- private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
- public static long copyLarge(Reader input, Writer output) throws IOException {
- char[] buffer = new char[DEFAULT_BUFFER_SIZE];
- long count = 0;
- int n = 0;
- while (-1 != (n = input.read(buffer))) {
- output.write(buffer, 0, n);
- count += n;
- }
- return count;
- }
/** * The default buffer size to use. */ private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; public static long copyLarge(Reader input, Writer output) throws IOException { char[] buffer = new char[DEFAULT_BUFFER_SIZE]; long count = 0; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; }
InputStream => InputStreamReader => Reader
StringWriter => Writer
注意此处用的StringWriter来实现字符输出展示
获取异常堆栈信息,也是使用StringWriter展示,如:
- import java.io.PrintWriter;
- import java.io.StringWriter;
- public class ExceptionUtils {
- public static String getStackTrace(Throwable t) {
- StringWriter sw = new StringWriter();
- PrintWriter pw = new PrintWriter(sw);
- try {
- t.printStackTrace(pw);
- return sw.toString();
- } finally {
- pw.close();
- }
- }
- }
import java.io.PrintWriter;import java.io.StringWriter;public class ExceptionUtils { public static String getStackTrace(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { t.printStackTrace(pw); return sw.toString(); } finally { pw.close(); } }}
阅读全文
0 0
- IOUtils的使用
- IOUtils和FileUtils的使用区别
- 使用IOUtils和FileUtils
- 工具类commons-io中的IOUtils的使用
- 使用apache的IOUtils类完成--文件下载(FileDownload)程序
- IOUtils的简单介绍
- IOUtils
- IOUtils
- IOUtils
- org.apache.commons.io.IOUtils IOUtils.copy与IOUtils.closeQuietly的用法
- 10.10-全栈Java笔记:Apache IOUtils和FileUtils的使用
- IOUtils 单元(7): TFile 结构的功能
- XE :IOUtils单元TDirectory的其他功能
- ioutils.ReadAll()会清空对应的reader
- Delphi的新工具单元IOUtils
- ioutils.ReadAll()会清空对应的reader
- Tomcat中使用commons-io-2.5发生的错误java.lang.ClassNotFoundException- org.apache.commons.io.IOUtils
- 使用Apache.commons.io文件工具类FileUtils,IOUtils使用
- 计算广告机制设计:基本的拍卖算法
- 跟我学Laravel之请求与输入
- CSS笔记——relative
- Linux C++ 获取某一进程的CPU占用率以及内存占用情况
- Arduino Proteus仿真
- IOUtils的使用
- 解决 IOS 显示 transparent 展示黑色透明
- python subprocess ctrl + c
- eclipse及idea下tomcat的安装
- 关于自己写的java.lang.String能不能被加载的问题
- 致敬平凡中伟大,失败的英雄--《敦刻尔克》
- Java IO-file类
- 编程常用链接
- JAVA 继承 父类子类 内存分配