01.
package
pruebas;
02.
import
java.io.BufferedWriter;
03.
import
java.io.File;
04.
import
java.io.FileWriter;
05.
import
java.io.IOException;
06.
07.
08.
09.
10.
11.
public
class
Escritor
implements
Runnable {
12.
private
String nombreFichero =
""
;
13.
@Override
14.
public
void
run() {
15.
System.out.println(
"Escritor ejecutando"
);
16.
ejecutaLogica(
0
);
17.
}
18.
public
Escritor(String nombreFichero) {
19.
System.out.println(
"Escritor inicializado"
);
20.
this
.nombreFichero = nombreFichero;
21.
}
22.
private
synchronized
void
ejecutaLogica(
int
numEjecucion) {
23.
System.out.println(numEjecucion);
24.
FileWriter fw =
null
;
25.
BufferedWriter bw =
null
;
26.
try
{
27.
File file =
new
File(nombreFichero);
28.
fw =
new
FileWriter(file);
29.
bw =
new
BufferedWriter(fw);
30.
for
(
int
i =
0
; i <
10
; i++) {
31.
String texto = numEjecucion +
":"
+ i;
32.
bw.write(texto +
"\n"
);
33.
}
34.
cierraRecursos(fw, bw);
35.
numEjecucion++;
36.
if
(numEjecucion <
2
)
37.
ejecutaLogica(numEjecucion);
38.
}
catch
(IOException e) {
39.
40.
e.printStackTrace();
41.
}
finally
{
42.
}
43.
}
44.
private
void
cierraRecursos(FileWriter fw, BufferedWriter bw) {
45.
try
{
46.
bw.close();
47.
}
catch
(IOException e) {
48.
49.
e.printStackTrace();
50.
}
51.
try
{
52.
fw.close();
53.
}
catch
(IOException e) {
54.
55.
e.printStackTrace();
56.
}
57.
}
58.
59.
60.
61.
62.
public
static
void
main(String[] args) {
63.
String nombreFichero =
"out.txt"
;
64.
Escritor escritor =
new
Escritor(nombreFichero);
65.
escritor.run();
66.
}
67.
}