`

file转String

    博客分类:
  • JAVA
F# 
阅读更多
public static String loadAFileToStringDE1(File f) throws IOException {    
    long beginTime = System.currentTimeMillis();  
    InputStream is = null;  
    String ret = null;  
    try {  
        is = new BufferedInputStream( new FileInputStream(f) );  
        long contentLength = f.length();  
        ByteArrayOutputStream outstream = new ByteArrayOutputStream( contentLength > 0 ? (int) contentLength : 1024);  
        byte[] buffer = new byte[4096];  
        int len;  
        while ((len = is.read(buffer)) > 0) {  
            outstream.write(buffer, 0, len);  
        }              
        outstream.close();  
        ret = outstream.toString();  
        //byte[] ba = outstream.toByteArray();  
        //ret = new String(ba);  
    } finally {  
        if(is!=null) {try{is.close();} catch(Exception e){} }  
    }  
    long endTime = System.currentTimeMillis();  
    System.out.println("方法1用时"+ (endTime-beginTime) + "ms");  
    return ret;          
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics