如何在Java中將實時數據寫入文件?將實時數據寫入文件
我在試圖獲得實時的twitter feed到文本文件。這裏是我已經寫了代碼:
public void onStatus(Status status)
{
User user = status.getUser();
BufferedWriter bufferedWriter = null;
try
{
bufferedWriter = new BufferedWriter(new FileWriter("c:\\twitterDumponFile.txt"));
String username = status.getUser().getScreenName();
bufferedWriter.write(username);
String profileLocation = user.getLocation();
bufferedWriter.write(profileLocation);
String content = status.getText();
bufferedWriter.write(content);
bufferedWriter.newLine();
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
//Closing the BufferedWriter
try
{
if (bufferedWriter != null)
{
bufferedWriter.flush();
bufferedWriter.close();
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
當我打開該文件twitterDumponFile.txt它包含數據的單行。每次我打開它,它都有一個不同的數據,但只有一行,它不會將新數據附加到文件上。
請幫我看看哪裏出錯了。
你試過這個'bufferedWriter = new FileWriter(「c:\\ twitterDumponFile.txt」,true)嗎? –