It's often useful to be able to append data to an existing file rather
than overwriting it. To do this just pass the boolean value true
as the second argument to the FileOutputStream()
constructor.
For example,
FileOutputStream fos = new FileOutputStream("16.html", true);
import java.io.*;
public class Append {
public static void main(String[] args) {
FileOutputStream[] fos = new FileOutputStream[args.length];
for (int i = 0; i < args.length; i++) {
try {
fos[i] = new FileOutputStream(args[i], true);
}
catch (IOException e) {
System.err.println(e);
}
} // end for
try {
while (true) {
int n = System.in.available();
if (n > 0) {
byte[] b = new byte[n];
int result = System.in.read(b);
if (result == -1) break;
for (int i = 0; i < args.length; i++) {
try {
fos[i].write(b, 0, result);
}
catch (IOException e) {
System.err.println(e);
}
} // end for
} // end if
} // end while
} // end try
catch (IOException e) {
System.err.println(e);
}
for (int i = 0; i < args.length; i++) {
try {
fos[i].close();
}
catch (IOException e) {
System.err.println(e);
}
} // end for
} // end main
}