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

时间:2025-02-21 21:31:43
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 = (buffer)) != -1){
           (buffer, 0, len);
       }
       data = ();        
} catch (Exception e) {
();
}finally{
try {  
          if(memStream != null){
();
}
if(bufferedInputStream != null){
();
}
           } catch (IOException e) {  
               ();  
           }  
}

String s = new String(data);
if(s != null){
bufferedWriter(s,"D:\\FtpFile\\");
        }
        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(() && ()){ 
                read = new InputStreamReader(new FileInputStream(file),"utf-8");
                BufferedReader bufferedReader = new BufferedReader(read);
                sb = new StringBuffer();
                String lineTxt = null;
                while((lineTxt = ()) != null){
                    (lineTxt);
                }
   }else{
       ("找不到指定的文件");
   }
        } catch (Exception e) {  
            ();  
        } finally {  
            if (read != null) {  
                try {  
                ();
                } catch (IOException e) {  
                    ();  
                }  
            }  
        }  
        
        if(sb != null){
        bufferedWriter((),"D:\\FtpFile\\");
        }
        return sb != null ? () : null; // ((), )
    }  

    /**
     * 缓冲字符写入文件,写字符串,数组或字符数据
     * @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);
  (content);
} catch (Exception e) {
();
} finally{
try {
if(bw != null){
(); 
}
} catch (IOException e) {
();
}
}
    }
    
    /**
     * 文件输出流,必须将数据转换为字节,并保存到文件
     * @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 = ();
  (contentInBytes);
  ();
} catch (Exception e) {
();
} finally{
  try {
   if (fop != null) {
    ();
   }
  } catch (IOException e) {
   ();
  }
}
    }
    
    /**
     * 测试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 = ();
String s = readTxtFileJson("D://FtpFile//get_services.txt");
//(s);
(()-date1);

// if((kubeData)){
// (()+"=="+());
// }

long date2 = ();
String s1 = readTxtFile("D://FtpFile//get_services.txt");
//(s1);
(()-date2);
} catch (Exception e) {
();
}
}