快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

java io读取文件到String

  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;       
    }
   

    public static String loadAFileToStringDE2(File f) throws IOException { 
        long beginTime = System.currentTimeMillis();
        InputStream is = null;
        String ret = null;
        try {
            is =  new FileInputStream(f) ;
            long contentLength = f.length();
            byte[] ba = new byte[(int)contentLength];
            is.read(ba);
            ret = new String(ba);
        } finally {
            if(is!=null) {try{is.close();} catch(Exception e){} }
        }
        long endTime = System.currentTimeMillis();
        System.out.println(\"方式2用时\"+ (endTime-beginTime) + \"ms\");
        return ret;       
    }   
   

 

    public static String loadAFileToStringDE3(File f) throws IOException {
        long beginTime = System.currentTimeMillis();
        BufferedReader br = null;
        String ret = null;
        try {
            br =  new BufferedReader(new FileReader(f));
            String line = null;
            StringBuffer sb = new StringBuffer((int)f.length());
            while( (line = br.readLine() ) != null ) {
                sb.append(line).append(LINE_BREAK);
            }
            ret = sb.toString();
        } finally {
            if(br!=null) {try{br.close();} catch(Exception e){} }
        }
        long endTime = System.currentTimeMillis();
        System.out.println(\"方式3用时\"+ (endTime-beginTime) + \"ms\");
        return ret;       
    }

3个方式去读取一个大于50M的文件,当不设置jvm参数时都OutofMemery,当设置-Xmx128M时。只有方式3 可以通过,设置到-Xmx256M时也只有方式3可以通过,干脆设置512M,都可以了,运行时间假如正常的话一般都在4~5S






返回类别: 教程
上一教程: 用java学习数据结构--单链表
下一教程: 取时间的小类

您可以阅读与"java io读取文件到String"相关的教程:
· 从JAVA档案(JAR)中读取文件
· JAVA:配置文件读取器
· JAVA读取配置文件例子
· java连接oracle,取io文件内容,子串替换
· 通过java提供的URL类包读取网上的文件
    微笑服务 优质保证 索取样品