java读取写入文件几种方式效率比较

public class ReadTxtJson {

public static String readTxtFile(String FileName) throws Exception {
BufferedInputStream bufferedInputStream = null;
ByteArrayOutputStream memStream = null;
byte[] data = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(FileName));
   memStream = new ByteArrayOutputStream();
       byte[] buffer = new byte[1024];
       int len = 0;
       while ((len = bufferedInputStream.read(buffer)) != -1){
           memStream.write(buffer, 0, len);
       }
       data = memStream.toByteArray();        
} catch (Exception e) {
e.printStackTrace();
}finally{
try {  
          if(memStream != null){
memStream.close();
}
if(bufferedInputStream != null){
bufferedInputStream.close();
}
           } catch (IOException e) {  
               e.getStackTrace();  
           }  
}

String s = new String(data);
if(s != null){
bufferedWriter(s,"D:\\FtpFile\\test1.txt");
        }
        return new String(data);
    }


/** 
     * 以行为单位读写文件内容 
     *  
     * @param filePath 
     */  
    public static String readTxtFileJson(String filePath) throws Exception{
        File file = new File(filePath);  
        InputStreamReader read = null;
        StringBuffer sb = null;
        try {  
        //判断文件是否存在
        if(file.isFile() && file.exists()){ 
                read = new InputStreamReader(new FileInputStream(file),"utf-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                sb = new StringBuffer();
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    sb.append(lineTxt);
                }
   }else{
       System.out.println("找不到指定的文件");
   }
        } catch (Exception e) {  
            e.getStackTrace();  
        } finally {  
            if (read != null) {  
                try {  
                read.close();
                } catch (IOException e) {  
                    e.getStackTrace();  
                }  
            }  
        }  
        
        if(sb != null){
        bufferedWriter(sb.toString(),"D:\\FtpFile\\test2.txt");
        }
        return sb != null ? sb.toString() : null; // GsonUtil.transJsonStrToObject(sb.toString(), KubeData.class)
    }  

    /**
     * 缓冲字符写入文件,写字符串,数组或字符数据
     * @param content
     * @throws Exception
     */
    public static void bufferedWriter(String content,String filePath) throws Exception{
    FileWriter fw = null;
    BufferedWriter bw = null;
    try {
  fw = new FileWriter(new File(filePath).getAbsoluteFile());
  bw = new BufferedWriter(fw);
  bw.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally{
try {
if(bw != null){
bw.close(); 
}
} catch (IOException e) {
e.printStackTrace();
}
}
    }
    
    /**
     * 文件输出流,必须将数据转换为字节,并保存到文件
     * @param content
     * @throws Exception
     */
    public static void fileOutputStream(String content,String filePath) throws Exception{
    FileOutputStream fop = null;
    try {
      fop = new FileOutputStream(new File(filePath));
  byte[] contentInBytes = content.getBytes();
  fop.write(contentInBytes);
  fop.flush();
} catch (Exception e) {
e.printStackTrace();
} finally{
  try {
   if (fop != null) {
    fop.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
}
    }
    
    /**
     * 测试1:
     * 文件大小:2m
     * 读取:readTxtFileJson 行读,写入:bufferedWriter 缓冲字符写入
     * 用时:51秒
     * 
     * 读取:readTxtFile 缓冲读取,写入:bufferedWriter 缓冲字符写入
     * 用时:31秒
     * 
     * 测试2:
     * 文件大小:10m
     * 读取:readTxtFileJson 行读,写入:fileOutputStream 文件输出流写入
     * 用时:501秒
     * 
     * 读取:readTxtFile 缓冲读取,写入:fileOutputStream 文件输出流写入
     * 用时:172秒
     * 
     * 文件大小:10m
     * 读取:readTxtFileJson 行读,写入:bufferedWriter 缓冲字符写入
     * 用时:293秒
     * 
     * 读取:readTxtFile 缓冲读取,写入:bufferedWriter 缓冲字符写入
     * 用时:132秒
     * 
     * 总结:
     * 不按格式读取效率高写入文件后大小比源文件小:readTxtFile 缓冲读取,bufferedWriter 缓冲字符写入
     * 按格式读取效率偏低(是第一种方式的一倍左右)写入文件后大小比源文件大小相当:readTxtFileJson 行读 ,bufferedWriter 缓冲字符写入
     * @param args
     */
public static void main(String[] args) {
try {
long date1 = System.currentTimeMillis();
String s = readTxtFileJson("D://FtpFile//get_services.txt");
//System.out.println(s);
System.out.println(System.currentTimeMillis()-date1);

// if(Util.isNotNull(kubeData)){
// System.out.println(kubeData.getKind()+"=="+kubeData.getApiVersion());
// }

long date2 = System.currentTimeMillis();
String s1 = readTxtFile("D://FtpFile//get_services.txt");
//System.out.println(s1);
System.out.println(System.currentTimeMillis()-date2);
} catch (Exception e) {
e.printStackTrace();
}
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值