LEER ARCHIVO .TXT

perrisimo
01 de Abril del 2008
HOLA AMIGOS TENGO UN PROBLEMA AL LEER UN ARCHIVO DE TEXTO .TXT, HE HECHO LO QUE ESTA RECOMENDADO EN ESTE MISMO FORO
InputStream is = getClass().getResourceAsStream("/prueba.txt");
char c;
while ((c = is.read()) != -1){
System.out.println((char)c);
}
is.close;
is = null;

Y NADA,ME SALE UN ERROR:

C:WTK25appsLeeArchivosrcLeeArchivo.java:34: not a statement
is.close;
^
1 error

ME PUEDEN AYUDAR???
TK.

kekomal
01 de Abril del 2008
is.close();

Sin paréntesis el compilador no entiende que sea una sentencia (not a statement).

perrisimo
01 de Abril del 2008
camarada ahora quiero leer una variable o cadena completa, o mejor aun auna estructura, no solo caracter por caracter, hay algun metodo para realizar esta accion?

lo que quiero es leer un archivo y almacenar en variables los valores que estan guardados en este archivo.

TK.

kekomal
01 de Abril del 2008
Para leer una línea de un fichero de texto también tienes que hacerlo carácter a carácter:

/**
* reads a single line from InputStreamReader
* @param in InputStreamReader used to read the line
* @throws IOException if there is any problem with reading
* @return the read line
*/
protected static String _readLine(InputStreamReader in) throws IOException {
StringBuffer strBuf = new StringBuffer("");
int i;
while ((i = in.read()) != -1) {
if ((char) i == \'\r\' || (char) i == \'\n\')
return strBuf.toString();
strBuf.append((char) i);
}
return strBuf.length() > 0 ? strBuf.toString() : null;
}

Si lo que quieres es representar estructuras más complejas, te aconsejo que las dispongas en un XML en lugar de en un txt clásico y que utilices algún parser como kXML.

kekomal
01 de Abril del 2008
Perdona, hubo un error con el Copy & Paste desde el Netbeans...

/**
* reads a single line from InputStreamReader
* @param in InputStreamReader used to read the line
* @throws IOException if there is any problem with reading
* @return the read line
*/
protected static String _readLine(InputStreamReader in) throws IOException {
StringBuffer strBuf = new StringBuffer("");
int i;
while ((i = in.read()) != -1) {
if ((char) i == 'r' || (char) i == 'n')
return strBuf.toString();
strBuf.append((char) i);
}
return strBuf.length() > 0 ? strBuf.toString() : null;
}