IOUtils的使用

[转载于http://uule.iteye.com/blog/2107010](http://uule.iteye.com/blog/2107010)

1、IOUtils.toString(InputStream input)

 

Java代码   收藏代码
  1. protected boolean writeEJBJar(String path, WebServiceProject project){  
  2.         String ejbJar = null;  
  3.         try {  
  4.             ejbJar = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(“META-INF/ws/ejb-jar.comm.template”));  
  5.               
  6.             logger.debug(”get template pass…”);  
  7.               
  8.             ejbJar = String.format(ejbJar, project.getName());  
  9.               
  10.             logger.debug(”template format :\n” + ejbJar);  
  11.               
  12.             FileUtils.writeStringToFile(new File(path), ejbJar, “UTF-8”);  
  13.               
  14.             logger.debug(”write ” + path + “ to harddrive pass…”);  
  15.         } catch (Exception e) {  
  16.             e.printStackTrace();  
  17.             logger.error(”writeBuild error : ” + e.getMessage());  
  18.             return false;  
  19.         }  
  20.         return true;  
  21.     }  
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;
    }

 

源码:

Java代码   收藏代码
  1. //注意此处用的StringWriter来实现字符输出展示  
  2. public static String toString(InputStream input) throws IOException {  
  3.         StringWriter sw = new StringWriter();  
  4.         copy(input, sw);  
  5.         return sw.toString();  
  6.     }  
//注意此处用的StringWriter来实现字符输出展示
public static String toString(InputStream input) throws IOException {
        StringWriter sw = new StringWriter();
        copy(input, sw);
        return sw.toString();
    }

 

Java代码   收藏代码
  1. public static void copy(InputStream input, Writer output)  
  2.             throws IOException {  
  3.         InputStreamReader in = new InputStreamReader(input);  
  4.         copy(in, output);  
  5.     }  
  6.   
  7.   
  8.     public static int copy(Reader input, Writer output) throws IOException {  
  9.         long count = copyLarge(input, output);  
  10.         if (count > Integer.MAX_VALUE) {  
  11.             return -1;  
  12.         }  
  13.         return (int) count;  
  14.     }  
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;
    }

 

Java代码   收藏代码
  1. /** 
  2.      * The default buffer size to use. 
  3.      */  
  4.     private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;  
  5.   
  6.       
  7.     public static long copyLarge(Reader input, Writer output) throws IOException {  
  8.         char[] buffer = new char[DEFAULT_BUFFER_SIZE];  
  9.         long count = 0;  
  10.         int n = 0;  
  11.         while (-1 != (n = input.read(buffer))) {  
  12.             output.write(buffer, 0, n);  
  13.             count += n;  
  14.         }  
  15.         return count;  
  16.     }  
/**
     * 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展示,如:

Java代码   收藏代码
  1. import java.io.PrintWriter;  
  2. import java.io.StringWriter;  
  3.   
  4. public class ExceptionUtils {  
  5.   
  6.     public static String getStackTrace(Throwable t) {  
  7.         StringWriter sw = new StringWriter();  
  8.         PrintWriter pw = new PrintWriter(sw);  
  9.         try {  
  10.             t.printStackTrace(pw);  
  11.             return sw.toString();  
  12.         } finally {  
  13.             pw.close();  
  14.         }  
  15.     }  
  16.   
  17. }  
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();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值