Foros del Web » Programación para mayores de 30 ;) » Java »

[SOLUCIONADO] Porque no imprime en el bucle

Estas en el tema de Porque no imprime en el bucle en el foro de Java en Foros del Web. Ayuda por favor! Estoy recibiendo datos de un archivo.txt pero los quiero enviar uno por uno a un textarea pero lo unico que hace es ...
  #1 (permalink)  
Antiguo 06/04/2016, 11:52
Avatar de Sexy10  
Fecha de Ingreso: abril-2016
Mensajes: 17
Antigüedad: 8 años
Puntos: 1
Exclamación Porque no imprime en el bucle

Ayuda por favor!
Estoy recibiendo datos de un archivo.txt pero los quiero enviar uno por uno a un textarea pero lo unico que hace es perar los 6 segundos y despues los imprime todos de golpe pregunta ¿como lo hago?
------------Codigo---------------------------------------------------------------------------------------
File f = new File("C:/victor.txt");
BufferedReader br;
try {
br= new BufferedReader( new FileReader( f ) );
String linea="";
while(br.ready()){
linea = br.readLine();
StringTokenizer st=new StringTokenizer(linea,"\n");
while (st.hasMoreElements()) {
String parametro = (String) st.nextElement();
try {
sleep(1000);
txtarecibir.setText(txtarecibir.getText()+parametr o+"\n");
} catch (InterruptedException ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
}
}catch (IOException e) {
JOptionPane.showMessageDialog(null, e);
}

Última edición por Sexy10; 06/04/2016 a las 11:58
  #2 (permalink)  
Antiguo 12/04/2016, 11:10
Avatar de Sexy10  
Fecha de Ingreso: abril-2016
Mensajes: 17
Antigüedad: 8 años
Puntos: 1
Respuesta: Porque no imprime en el bucle

// me costo pero aqui esta por si alguien lo necesita!!
Código Java:
Ver original
  1. Runnable miRunnable = new Runnable()
  2.       {
  3.          public void run()
  4.          {
  5.             try
  6.             { File f = new File("C:/victor.txt");
  7. try {
  8. br= new BufferedReader( new FileReader( f ) );
  9. String linea="";
  10. while(br.ready()){
  11. linea = br.readLine();
  12. StringTokenizer st=new StringTokenizer(linea,"\n");
  13.        while (st.hasMoreElements()) {
  14.        String parametro = (String) st.nextElement();
  15.         Thread.sleep(2000);
  16.         txtaenviar.setText(parametro);
  17.        }
  18. }
  19. }catch (IOException e) {
  20. JOptionPane.showMessageDialog(null, e);
  21. }
  22.  System.out.println("Terminé");
  23.             }
  24.             catch (Exception e)
  25.             {
  26.                e.printStackTrace();
  27.             }
  28.          }
  29.       };
  30.       Thread hilo = new Thread (miRunnable);
  31.       hilo.start();

Etiquetas: bucle, imprime, string
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 08:11.