I am trying to create a file through the FileOutputStream stream, if I use the for-loop, it does it correctly:
public class EjemploFileOutputStream {
/**
* @param args
*/
public static void main(String[] args) {
OutputStream fOut = null;
try {
fOut = new FileOutputStream("primero.dat");
for(int i = 0; i < 1000; i++) {
fOut.write(i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fOut != null)
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
but using the Stream API and Lambda expressions, it gives me an error
this would be the code:
public class EjemploFileOutputStream {
static OutputStream fOut2 = null;
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
List<Number> cantidades = Arrays.asList(10, 20, 30, 40, 50);
try {
fOut2 = new FileOutputStream("primero3.dat");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
cantidades.stream().map(cantidad->cantidad.intValue()).forEach(cantidad -> {
try {
fOut2.write(cantidad);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if (fOut2 != null)
try {
fOut2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
the error trace in the console (Eclipse):
the sequence in debugger mode: The first value of the list enters apparently OK , cantidad=10
The next value in the list enters apparently OK, cantidad=20:
but when processing it, it catches the error exception:
I am trying different options, but while I wanted to know could any of you know how to avoid this error



