I have a little problem with a function I made. I want that everytime I give a string to this function, it will save me to a new Line in the same file, but actually now is saving only the last string Im givving. It's like overwriting again and again, Need some help
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();
}
For example in BufferedWriter Has a method called newLine that helps to do that.
But since I want to use GZIPOutputStream class I need BufferedOutputStream.
Any ideeas how to do it? Thank ypu
You are exactly right, you overwrite the file. If you open it with an FileOutputStream
it will start from the beginning. You can either keep the stream open or use the append mode by specifying true
after the file (name).
BufferedOutputStream out = new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream(newFile, true)
)
);
With the GZIPOutputStream you have quite some overhead if you open a new stream on each line, but it is defined to work this way. (Again: keeping it open also helps with this).