Contenido de certificación
Buscar
Social
Ofertas laborales ES
« Más sobre GregorianCalendar.... | Main | GregorianCalendar en Java »
jueves
abr032014

Pregunta sobre Java

¿Cuál es la salida por consola, y cuál la escrita en el fichero, de ejecutar el siguiente código fuente?

NOTA: tiene más gracia contestar sin hacer uso de IDE que haciendo uso de IDE

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. * @author Jaime Carmona Loeches
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.// TODO Auto-generated catch block
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.// TODO Auto-generated catch block
49.e.printStackTrace();
50.}
51.try {
52.fw.close();
53.} catch (IOException e) {
54.// TODO Auto-generated catch block
55.e.printStackTrace();
56.}
57.}
58./**
59.* @param args
60.* @throws InterruptedException
61.*/
62.public static void main(String[] args) {
63.String nombreFichero = "out.txt";
64.Escritor escritor = new Escritor(nombreFichero);
65.escritor.run();
66.}
67.}

Reader Comments (2)

Escritor inicializado Escritor ejecutando 0 1 out.txt 1 : 0 1 : 1 1 : 2 1 : 3 1 : 4 1 : 5 1 : 6 1 : 7 1 : 8 1 : 9

abril 4, 2014 | Unregistered Commenterjose

Hola José, gracias por tu respuesta. ¿Puedes argumentar la respuesta por favor?
Un saludo,

abril 5, 2014 | Registered Commenterjcarmonaloeches

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>