LEER ARCHIVO .TXT
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.
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.
is.close();
Sin paréntesis el compilador no entiende que sea una sentencia (not a statement).
Sin paréntesis el compilador no entiende que sea una sentencia (not a statement).
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.
lo que quiero es leer un archivo y almacenar en variables los valores que estan guardados en este archivo.
TK.
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.
/**
* 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.
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;
}
/**
* 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;
}
