
package lecturapaginaweb;

import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class LecturaPaginaWeb {

    public static void main(String[] args) {
      try {
            URL url = new URL("http://www.aemet.es/xml/municipios/localidad_03082.xml");
            URLConnection  uRLConnection = url.openConnection();
            uRLConnection.connect();               
            InputStream inputStream = uRLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            FileWriter fileWriter = new FileWriter(new File("D:\\archivos_creados_Java\\recurso_web.txt"));
            PrintWriter printWriter = new PrintWriter(fileWriter);
         
            boolean escrituraEnFichero = false;      
         
            char[] buffer = new char[1000];
            int numeroCaracteres;
            while ((numeroCaracteres = bufferedReader.read(buffer)) > 0) {
                if (escrituraEnFichero)
                    printWriter.println(new String(buffer, 0, numeroCaracteres));
                else             
                    System.out.println(new String(buffer, 0, numeroCaracteres));
            }
         
            if (escrituraEnFichero)
                fileWriter.close();
         
      } catch (Exception excepcion) {
             System.out.println(excepcion.getMessage());        
      }   
    }  
}
