赞
踩
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; }
源码:
//注意此处用的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;
}
/** * 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(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。