1. 程式人生 > >java讀取寫入檔案幾種方式效率比較

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