Probando las clases de entrada y salida de ficheros, tras crear un archivo mediante DataOutputStream, he visto que en la API de [URL="http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataInputStream.html"]DataInputStream[/URL] aparece lo siguiente
Cita: String readLine()
Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));
El caso es que no consigo que aparezca nada al leer una línea utilizando .readLine(). El archivo de datos es correcto ya que sí puedo leerlo mediante los .read(), pero con readLine() no se muestra el resultado.
Éste es el código, y he probado con las dos variantes que aparecen en rojo, con idéntico resultado
Cita: try {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("src/DataOutput.txt"));
dos.writeChars("Probandocadena");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
// Open the file that is the first
// command line parameter
InputStreamReader isr = new InputStreamReader(new DataInputStream(new FileInputStream ("src/DataOutput.txt")));
//InputStreamReader isr = new InputStreamReader(new FileInputStream ("src/DataOutput.txt"));
BufferedReader br = new BufferedReader(isr);
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
br.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
Obsevando la variable strLine en el debug, parece que sí lee el fichero, pero no es capaz de convertirlo en objeto String.
Entrando más en detalles, en los value de una String normal aparecería [0] = P [1] = r ... mientras que en la cadena recuperada con .readLine, tiene la forma [0] = [1] = P [2] = [3] = r
Saludos y gracias.