Siempre se me ha presentado este problema, cada vez que leo un archivo desde java, y este contiene caracteres como la Ñ, estos son tomado como símbolos raros:
Voy a mostrar el código que ocasiona esta distorsión, el archivo "D:\temporal\prueba.txt", cuyo contenido es:
PAÑALES DESCARTABLES
ÚTILES DE LIMPIEZA
VINO AÑEJO
Y el código es el siguiente:
import java.io.*;
/**
*
* @author mzavaleta
*/
public class TestÑ {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
FileInputStream fis = new FileInputStream("D:/temporal/prueba.txt");
InputStreamReader is = new InputStreamReader(fis);
BufferedReader bf = new BufferedReader(is);
String linea;
while ((linea = bf.readLine()) != null) {
System.out.println(linea);
}
bf.close();
is.close();
fis.close();
}
}
La salida es la siguiente:
PA�ALES DESCARTABLES
�TILES DE LIMPIEZA
VINO A�EJO
Pero si cambiamos la línea que dice:
InputStreamReader is = new InputStreamReader(fis);
Por
InputStreamReader is = new InputStreamReader(fis, "ISO-8859-1");
Se soluciona el problema y la salida es:
PAÑALES DESCARTABLES
ÚTILES DE LIMPIEZA
VINO AÑEJO
Espero que les sirva, el código final sería:
import java.io.*;
/**
*
* @author mzavaleta
*/
public class TestÑ {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
FileInputStream fis = new FileInputStream("D:/temporal/prueba.txt");
InputStreamReader is = new InputStreamReader(fis, "ISO-8859-1");
BufferedReader bf = new BufferedReader(is);
String linea;
while ((linea = bf.readLine()) != null) {
System.out.println(linea);
}
bf.close();
is.close();
fis.close();
}
}
Voy a mostrar el código que ocasiona esta distorsión, el archivo "D:\temporal\prueba.txt", cuyo contenido es:
PAÑALES DESCARTABLES
ÚTILES DE LIMPIEZA
VINO AÑEJO
Y el código es el siguiente:
import java.io.*;
/**
*
* @author mzavaleta
*/
public class TestÑ {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream fis = new FileInputStream("D:/temporal/prueba.txt");
InputStreamReader is = new InputStreamReader(fis);
BufferedReader bf = new BufferedReader(is);
String linea;
while ((linea = bf.readLine()) != null) {
System.out.println(linea);
}
bf.close();
is.close();
fis.close();
}
}
La salida es la siguiente:
PA�ALES DESCARTABLES
�TILES DE LIMPIEZA
VINO A�EJO
Pero si cambiamos la línea que dice:
InputStreamReader is = new InputStreamReader(fis);
Por
InputStreamReader is = new InputStreamReader(fis, "ISO-8859-1");
Se soluciona el problema y la salida es:
PAÑALES DESCARTABLES
ÚTILES DE LIMPIEZA
VINO AÑEJO
Espero que les sirva, el código final sería:
import java.io.*;
/**
*
* @author mzavaleta
*/
public class TestÑ {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
FileInputStream fis = new FileInputStream("D:/temporal/prueba.txt");
InputStreamReader is = new InputStreamReader(fis, "ISO-8859-1");
BufferedReader bf = new BufferedReader(is);
String linea;
while ((linea = bf.readLine()) != null) {
System.out.println(linea);
}
bf.close();
is.close();
fis.close();
}
}
Ok, bueno saberlo ...
ResponderEliminarMe ayudo mucho, esta facil de enterder pues lo explicas muy bien Gracias
ResponderEliminarParece fácil pero no me atrvo a usarlo, puedo dejar un desaguisado
ResponderEliminarGracias por compartirlo, fue de gran ayuda.
ResponderEliminarYo lo he usado con una lectura Scanner y también me ha funcionado:
ResponderEliminarScanner lee = new Scanner (System.in, "ISO-8859-1");
Muy útil, muchas gracias!
Muchas gracias !!
ResponderEliminarMuchisimas Gracias Te amo compa
ResponderEliminarMuchas gracias por el tip, muy util!
ResponderEliminarEres la polla con cebolla, te amo
ResponderEliminar