Advertisement
andgau

Problemas Encoding

Mar 10th, 2014
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package es.sinjavalabs;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.StringWriter;
  12. import java.nio.charset.Charset;
  13. import java.util.Properties;
  14.  
  15. public class Soyelamain {
  16.  
  17.     public static void main(String[] args) {
  18.         Properties propertiesOrigen = new Properties();
  19.         propertiesOrigen.put("valor1", "España");
  20.         propertiesOrigen.put("valor2", " ¿Elefante?");
  21.         propertiesOrigen.put("valor3", "Canción");
  22.         propertiesOrigen.put("valor4", "Ánimo");
  23.  
  24.         String fileutf = "C:/Work/soyutf.xml";
  25.         String fileiso = "C:/Work/soyiso.xml";
  26.  
  27.         escritura(propertiesOrigen, fileutf, fileiso);
  28.         Properties propertiesUTF8 = new Properties();
  29.         lecturaXml(fileutf, propertiesUTF8);
  30.         try {
  31.             FileReader fr = new FileReader(fileutf);
  32.             BufferedReader br = new BufferedReader(fr);
  33.             StringWriter sw = new StringWriter();
  34.             String currentLine = br.readLine();
  35.             while (currentLine != null) {
  36.                 sw.append(currentLine);
  37.                 currentLine = br.readLine();
  38.             }
  39.             System.out.println("Lectura " + sw.toString());
  40.         } catch (Exception e) {
  41.             e.printStackTrace();
  42.         }
  43.  
  44.         try {
  45.             FileReader fr = new FileReader(fileiso);
  46.             BufferedReader br = new BufferedReader(fr);
  47.             StringWriter sw = new StringWriter();
  48.             String currentLine = br.readLine();
  49.             while (currentLine != null) {
  50.                 sw.append(currentLine);
  51.                 currentLine = br.readLine();
  52.             }
  53.  
  54.             System.out.println("Lectura Chunga " + sw.toString());
  55.  
  56.         } catch (Exception e) {
  57.             e.printStackTrace();
  58.         }
  59.  
  60.         System.out
  61.                 .println("______________Segundo intento_______________________ ");
  62.  
  63.         try {
  64.             InputStream fis = new FileInputStream(fileiso);
  65.             InputStreamReader isr = new InputStreamReader(fis,
  66.                     Charset.forName("ISO-8859-1"));
  67.             BufferedReader br = new BufferedReader(isr);
  68.             StringWriter stringWriter = new StringWriter();
  69.             String currentLine = br.readLine();
  70.             while (currentLine != null) {
  71.                 stringWriter.append(currentLine);
  72.                 currentLine = br.readLine();
  73.             }
  74.             System.out.println("Lectura Buena " + stringWriter.toString());
  75.         } catch (Exception e) {
  76.             e.printStackTrace();
  77.         }
  78.     }
  79.  
  80.     private static void lecturaXml(String fileutf, Properties propertiesUTF8) {
  81.         InputStream is;
  82.         try {
  83.             InputStream input = new FileInputStream(fileutf);
  84.             propertiesUTF8.loadFromXML(input);
  85.             input.close();
  86.  
  87.             System.out.println("Propiedad uno UTF "
  88.                     + propertiesUTF8.getProperty("valor1"));
  89.  
  90.             Properties propertiesISO = new Properties();
  91.  
  92.             is = new FileInputStream(fileutf);
  93.             propertiesISO.loadFromXML(is);
  94.             is.close();
  95.             System.out.println("Propiedad uno ISO "
  96.                     + propertiesISO.getProperty("valor1"));
  97.  
  98.         } catch (Exception e) {
  99.             e.printStackTrace();
  100.  
  101.         }
  102.  
  103.     }
  104.  
  105.     private static void escritura(Properties propertiesOrigen, String fileutf,
  106.             String fileiso) {
  107.         FileOutputStream fo;
  108.         try {
  109.             fo = new FileOutputStream(fileutf);
  110.             propertiesOrigen.storeToXML(fo, "Cómo mola UTF-8", "UTF-8");
  111.             fo = new FileOutputStream(fileiso);
  112.             propertiesOrigen.storeToXML(fo, "Cómo mola ISO", "ISO-8859-1");
  113.         } catch (Exception e) {
  114.             e.printStackTrace();
  115.         }
  116.     }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement