IOUtils的使用

来源:互联网 发布:pscc2018破解软件 编辑:程序博客网 时间:2024/05/22 13:01
[转载于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();        }    }}
原创粉丝点击