[英]BufferedOutputStream multiple lines Write in File
我做的功能有點問題。 我希望每次給該函數提供一個字符串時,它將把我保存到同一文件中的新行中,但是實際上現在只保存了我給的最后一個字符串。 就像一次又一次的覆蓋,需要一些幫助
public void WritingGZFile(String directory, String linesWithPattern, String newFile) throws IOException
{
newFile = directory + '\\' + newFile;
BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(
newFile)));
out.write(linesWithPattern.getBytes());
out.write("\r\n".getBytes());
out.close();
}
例如在BufferedWriter中有一個名為newLine的方法可以幫助實現這一點。
但是由於我要使用GZIPOutputStream類,因此需要BufferedOutputStream。
有什么想法嗎? 感謝ypu
完全正確,您覆蓋了文件。 如果使用FileOutputStream
打開它,它將從頭開始。 您可以通過在文件(名稱)后指定true
來保持流打開或使用附加模式 。
BufferedOutputStream out = new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream(newFile, true)
)
);
使用GZIPOutputStream,如果在每一行上打開一個新的流,您將有相當大的開銷,但是它被定義為以這種方式工作。 (再次:保持打開狀態對此也有幫助)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.