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

[SOLUCIONADO] De PHP a Java

Estas en el tema de De PHP a Java en el foro de Java en Foros del Web. Hola de nuevo amigos, encontre un codigo que me seria muy util pero lo necesito en Java y no se php, alguien lo podria traducir? ...
  #1 (permalink)  
Antiguo 29/03/2013, 21:33
Avatar de eagle_knight  
Fecha de Ingreso: marzo-2013
Mensajes: 14
Antigüedad: 11 años, 1 mes
Puntos: 0
Pregunta De PHP a Java

Hola de nuevo amigos, encontre un codigo que me seria muy util pero lo necesito en Java y no se php, alguien lo podria traducir?...

<?php
set_time_limit(0);
$fpa = fopen("content.txt","r");
$fpa = fgets($fpa);
while(1){
$fp = fopen("content.txt","r");
$fp = fgets($fp);
if($fp != $fpa){
echo $fp;
break;
}
sleep(1);
}
?>
  #2 (permalink)  
Antiguo 29/03/2013, 22:10
 
Fecha de Ingreso: diciembre-2011
Mensajes: 152
Antigüedad: 12 años, 4 meses
Puntos: 34
Respuesta: De PHP a Java

Seria algo como esto:
Código java:
Ver original
  1. private static final String FILE = "E:/example.txt";
Código java:
Ver original
  1. Thread thread = new Thread(new Runnable() {
  2.     @Override
  3.     public void run() {
  4.         String content = getContent(FILE);
  5.         while (true) {
  6.             String actualContent = getContent(FILE);
  7.             if (!content.equals(actualContent)) {
  8.                 System.out.printf("Nuevo contenido: %s\n", actualContent);
  9.                 break;
  10.             }
  11.             try {
  12.                 Thread.sleep(1);
  13.             } catch (InterruptedException ex) {
  14.                 //tu logger o manejo de excepciones
  15.             }
  16.         }
  17.     }
  18. }, this.getClass().getSimpleName() + " - Thread");
  19. thread.start();

Función getContent:
Código java:
Ver original
  1. public static String getContent(String file) {
  2.     BufferedReader bufferedReader = null;
  3.     String content = "";
  4.     try {
  5.         bufferedReader = new BufferedReader(new FileReader(file));
  6.         String line;
  7.         while (null != (line = bufferedReader.readLine())) {
  8.             content += line;
  9.         }
  10.     } catch (FileNotFoundException ex) {
  11.         //tu logger o manejo de excepciones
  12.     } catch (IOException ex) {
  13.         //tu logger o manejo de excepciones
  14.     } finally {
  15.         if (null != bufferedReader) {
  16.             try {
  17.                 bufferedReader.close();
  18.             } catch (IOException ex) {
  19.                 //tu logger o manejo de excepciones
  20.             }
  21.         }
  22.     }
  23.     return content;
  24. }
  #3 (permalink)  
Antiguo 29/03/2013, 22:24
Avatar de eagle_knight  
Fecha de Ingreso: marzo-2013
Mensajes: 14
Antigüedad: 11 años, 1 mes
Puntos: 0
Respuesta: De PHP a Java

Cita:
Iniciado por LuisChavezB Ver Mensaje
Seria algo como esto:
Código java:
Ver original
  1. private static final String FILE = "E:/example.txt";
Código java:
Ver original
  1. Thread thread = new Thread(new Runnable() {
  2.     @Override
  3.     public void run() {
  4.         String content = getContent(FILE);
  5.         while (true) {
  6.             String actualContent = getContent(FILE);
  7.             if (!content.equals(actualContent)) {
  8.                 System.out.printf("Nuevo contenido: %s\n", actualContent);
  9.                 break;
  10.             }
  11.             try {
  12.                 Thread.sleep(1);
  13.             } catch (InterruptedException ex) {
  14.                 //tu logger o manejo de excepciones
  15.             }
  16.         }
  17.     }
  18. }, this.getClass().getSimpleName() + " - Thread");
  19. thread.start();

Función getContent:
Código java:
Ver original
  1. public static String getContent(String file) {
  2.     BufferedReader bufferedReader = null;
  3.     String content = "";
  4.     try {
  5.         bufferedReader = new BufferedReader(new FileReader(file));
  6.         String line;
  7.         while (null != (line = bufferedReader.readLine())) {
  8.             content += line;
  9.         }
  10.     } catch (FileNotFoundException ex) {
  11.         //tu logger o manejo de excepciones
  12.     } catch (IOException ex) {
  13.         //tu logger o manejo de excepciones
  14.     } finally {
  15.         if (null != bufferedReader) {
  16.             try {
  17.                 bufferedReader.close();
  18.             } catch (IOException ex) {
  19.                 //tu logger o manejo de excepciones
  20.             }
  21.         }
  22.     }
  23.     return content;
  24. }
Muchas gracias! ahora le entiendo perfectamente

Etiquetas: php
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 06:12.