OK - quick question for everyone. I've solved the task - but when I tested the .close() method with a file of my own... it wrote the string to be appended TWICE
I can't for the life of me figure out why... any ideas?
package com.codegym.task.task18.task1813;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
AmigoOutputStream
*/
public class AmigoOutputStream extends FileOutputStream {
public static String fileName = "[filename]";
private FileOutputStream original;
public AmigoOutputStream(FileOutputStream original) throws IOException {
super(original.getFD());
this.original = original;
}
@Override
public void flush() throws IOException {
original.flush();
}
@Override
public void write(byte[] bytes) throws IOException {
original.write(bytes);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
original.write(b, off, len);
}
@Override
public void write (int b) throws IOException {
original.write(b);
}
@Override
public void close() throws IOException {
original.flush();
String toAppend = "CodeGym © All rights reserved.";
write(toAppend.getBytes());
original.close();
}
public static void main(String[] args) throws IOException {
AmigoOutputStream amigoStream = new AmigoOutputStream(new FileOutputStream(fileName));
amigoStream.close();
}
}