
package lecturapaginawebII;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class LecturaPaginaWebII {
 
    public static void main(String[] args) {
            
       try {  
             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new URL("http://www.aemet.es/xml/municipios/localidad_03082.xml").openStream(), StandardCharsets.ISO_8859_1));
             OutputStreamWriter fileWriter = new OutputStreamWriter(new FileOutputStream(new File("D:\\archivos_creados_Java\\recurso_web.txt")) , StandardCharsets.ISO_8859_1);            
             PrintWriter printWriter = new PrintWriter(fileWriter);

             boolean escrituraEnFichero = false;   
         
             String cadenaLeida;
             while((cadenaLeida  = bufferedReader.readLine()) != null) {
                 if (escrituraEnFichero)
                     printWriter.println(cadenaLeida);
                 else
                     System.out.println(cadenaLeida);  
             }

             if (escrituraEnFichero)
                 fileWriter.close();
             
           }  catch(Exception e) 
              {System.out.println(e);}                   
    } 
}
