2017-10-19 43 views
-1

我的java代碼接收像twitter這樣的流數據。我需要存儲數據,例如每個文件10000條記錄。所以,我需要重新創建file writerbuffered writer以創建一個新文件,然後在其上寫入數據。java.io.IOException:流關閉。將流數據寫入多個文件的最佳方式是什麼?

// global variables 
    String stat; 
    long counter = 0; 
    boolean first = true; 
    Date date; 
    SimpleDateFormat format; 
    String currentTime; 
    String fileName; 
    BufferedWriter bw = null; 
    FileWriter fw = null; 

    public static void main(String[] args) { 
     String dirToSave = args[0]; 
     String fileIdentifier = args[1]; 

     createFile(dirToSave, fileIdentifier); 

     StatusListener listener = new StatusListener() { 
      @Override 
      public void onStatus(Status status) { 
       stat = TwitterObjectFactory.getRawJSON(status); 

       try { 
        if(bw!=null){ 
         bw.write(stat + "\n"); 
        } 
       } catch (IOException ex) { 
        System.out.println(ex.getMessage()); 
       } 
       counter++; 

       if (counter == 10000) { 
        createFile(dirToSave, fileIdentifier); 
        try { 
         TimeUnit.SECONDS.sleep(5); 
        } catch (InterruptedException ex) { 
         System.out.println(ex.getMessage()); 
        } 
        counter = 0; 
       } 
      } 
     }; 

TwitterStream twitterStream = new TwitterStreamFactory(confBuild.build()).getInstance(); 

    twitterStream.addListener(listener); 

    // twitterStream.filter(filQuery); 
    } 

public static void createFile(String path, String fileIdentifier) { 
     date = new Date(); 
     format = new SimpleDateFormat("yyyyMMddHHmm"); 
     currentTime = format.format(date); 
     fileName = path + "/" + fileIdentifier + currentTime + ".json"; 

// if there was buffer before, flush & close it first before creating new file 
     if (!first) { 
      try { 
       bw.flush(); 
       bw.close(); 
       fw.close(); 
      } catch (IOException ex) { 
       Logger.getLogger(LocalFile_All_en.class 
         .getName()).log(Level.SEVERE, null, ex); 
      } 
     } else { 
      first = false; 
     } 

     // create a new file 
     try { 
      fw = new FileWriter(fileName); 
      bw = new BufferedWriter(fw); 
     } catch (IOException ex) { 
      Logger.getLogger(Stack.class 
        .getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

但是,我總是在幾個小時後出錯。

SEVERE: null 
java.io.IOException: Stream closed 

編輯:該錯誤消息說,這些代碼拋出錯誤

if (counter == 10000) { 
        createFile(dirToSave, fileIdentifier); 
... 

bw.flush(); 

什麼是我的代碼的問題嗎?還是有更好的方式來寫這樣的流數據?

+0

它是一個多線程程序嗎? – Alex

+0

@Alex不,它只有一個線程。接收流數據並存儲到文件。 –

+0

如何調用'onStatus'方法?哪一行引發異常? – Alex

回答

1

如果此錯誤時不時出現,並且在此錯誤再次確定後再次寫入,我認爲可能會發生這樣的情況:bw已關閉,但尚未重新打開,而onStatus()嘗試寫入刷新它。

因此bw可以不爲null但是關閉。您需要以某種方式同步關閉/打開。

例如,在onStatus()這樣的東西,使你不只是直接寫入bw,但與一些回調處理關閉/重新打開新文件。

更新:假設這裏twitterStream可以調用onStatus()而不必等待上一次調用完成。第一個電話剛剛關閉了流,第二個電話剛剛寫完。很少見,但會在很長一段時間內發生。

更新2:這也適用於flush()部分。

我已經添加了這個也作爲一個簡短的評論,但人們經常告訴擺脫靜態,特別是在Java爭論中的全局靜態,它會導致後來很難解決/調試的大問題。這可能是很好的例子。

閱讀也:

Why are static variables considered evil?

Volatile Vs Static in java

後者有一個例子如何sychronize併發請求。

相關問題