Метод close() не делегирует свое выполнение классу FileOutputStream.
Метод close() не вызывает flush() у объекта FileOutputStream.
package com.javarush.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 = "C:/tmp/result.txt";
private FileOutputStream original;
String javarush = "JavaRush © All rights reserved.";
public static void main(String[] args) throws FileNotFoundException {
new AmigoOutputStream(new FileOutputStream(fileName));
}
public AmigoOutputStream (FileOutputStream fos) throws FileNotFoundException {
super(fileName);
this.original = fos;
}
@Override
public void write(int b) throws IOException {
original.write(b);
}
@Override
public void write(byte[] b) throws IOException {
original.write(b);
}
@Override
public void flush() throws IOException {
original.flush();
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
original.write(b, off, len);
}
@Override
public void close() throws IOException {
original.flush();
original.write(javarush.getBytes());
original.close();
}
}