
import java.io.BufferedReader;Al correr este código se imprime lo siguiente en la consola de Eclipse:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.io.FileReader;
public class Lector {
public static void main(String[] args) {
File f = new File( "\\codigos.txt" );
BufferedReader entrada;
try {
entrada = new BufferedReader( new FileReader( f ) );
String codigo;
int eg=0,ba=0,ti=0,su=0,re=0 ;
while(entrada.ready()){
codigo = entrada.readLine();
URL url = new URL("http://www.orce.uni.edu.pe/detaalu.php?id="+codigo+"&op=detalu");
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = bufferedReader.readLine();
String nombre = null;
String condicion = null;
while (line != null) {
if (line.equals(" <td style=\"font-weight: bold;\"><img src=\"/images/flechita.gif\"> Nombres:</td>")){
line = bufferedReader.readLine();
nombre = line.replace("", "").replace(" ", "").replace(" ", "-").replace("", "");
if(nombre.charAt(nombre.length()-1)=='-'){
nombre= nombre.replace(nombre, nombre.substring(0,nombre.length()-1));
}
}
if (line.equals(" <td style=\"font-weight: bold;\"><img src=\"/images/flechita.gif\"> Situación:</td>")){
line = bufferedReader.readLine();
condicion = line.replace("", "").replace(" ", "");
if (condicion.equals("BACHILLER"))
ba=ba+1;
if (condicion.equals("SUSPENSION VOLUNTARIA"))
su=su+1;
if (condicion.equals("EGRESADO"))
eg=eg+1;
if (condicion.equals("TITULADO"))
ti=ti+1;
if (condicion.equals("EN REGULARIZACION DE CURSO(S)"))
re=re+1;
}
line = bufferedReader.readLine();
}
bufferedReader.close();
System.out.println(nombre+": "+condicion);
}
System.out.println("");
System.out.println("RESULTADOS");
System.out.println("");
System.out.println("TITULADOS: "+ti);
System.out.println("BACHILLERES: "+ba);
System.out.println("EGRESADOS: "+eg);
System.out.println("EN REGULARIACION DE CURSO: "+re);
System.out.println("SUSPENSIONES VOLUNTARIAS: "+su);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}}}


Nota: Por si acaso, descarga la fuente aquí. Usa tu propio .txt, no lo olvides.
No hay comentarios.:
Publicar un comentario