Otra vez con este problema...
Hola.
Mi dominio de Java es cero patatero y lleva ya unos dÃas atascado en este problema:
Resulta que quiero escribir un programa que lea un fichero de nombre "escrito.txt" y lo haga aparecer en pantalla. Pero con una particularidad, y es que si lee un caracter '$' ha de ignorarlo asà como el resto de la lÃnea.
Leà por ahà que esto se podÃa hacer facilmente con readLine, pero no termino de aclararme.
Tengo dos códigos fuente alternativos pero ninguno de los dos me funciona y me gustarÃa que alguno me dijeseis donde está el fallo o qué otro algoritmo podrÃa utilizar para poder hacer funcionar el programa.
Los códigos fuente serÃan:
Prueba.java --->
-----------------------------------------------------------------------
import java.io.*;
class Prueba {
public static void main(String[] args) {
try {
File fich = new File("escrito.txt");
FileInputStream escrito = new FileInputStream(fich);
int cont;
int c;
cont = 0;
String tex = new String("");
bucle1:
while ((c = escrito.read()) != -1) {
if (c == (int)'$') {
c = escrito.read();
while (c != (int)'n') {
c = escrito.read();
}
continue bucle1;
}
else {
tex = tex + (char)c;
cont++;
}
}
System.out.println(tex);
System.out.println(cont);
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
}
-----------------------------------------------------------------------
Prueba2.java --->
------------------------------------------------------------------------
import java.io.*;
class Prueba2 {
public static void main(String[] args) {
try {
File fich = new File("escrito.txt");
FileInputStream sud = new FileInputStream(fich);
int cont;
int c;
cont = 0;
boolean sigue = true;
String tex = new String("");
bucle1:
while ((c = escrito.read()) != -1) {
if (c == (int)'$') {
sigue = false;
}
else {
sigue = true;
}
if (sigue) {
tex = tex + (char)c;
cont++;
}
}
System.out.println(tex);
System.out.println(cont);
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
}
-----------------------------------------------------------------------
Muchas gracias de antemano y perdón por el tostón.
Mi dominio de Java es cero patatero y lleva ya unos dÃas atascado en este problema:
Resulta que quiero escribir un programa que lea un fichero de nombre "escrito.txt" y lo haga aparecer en pantalla. Pero con una particularidad, y es que si lee un caracter '$' ha de ignorarlo asà como el resto de la lÃnea.
Leà por ahà que esto se podÃa hacer facilmente con readLine, pero no termino de aclararme.
Tengo dos códigos fuente alternativos pero ninguno de los dos me funciona y me gustarÃa que alguno me dijeseis donde está el fallo o qué otro algoritmo podrÃa utilizar para poder hacer funcionar el programa.
Los códigos fuente serÃan:
Prueba.java --->
-----------------------------------------------------------------------
import java.io.*;
class Prueba {
public static void main(String[] args) {
try {
File fich = new File("escrito.txt");
FileInputStream escrito = new FileInputStream(fich);
int cont;
int c;
cont = 0;
String tex = new String("");
bucle1:
while ((c = escrito.read()) != -1) {
if (c == (int)'$') {
c = escrito.read();
while (c != (int)'n') {
c = escrito.read();
}
continue bucle1;
}
else {
tex = tex + (char)c;
cont++;
}
}
System.out.println(tex);
System.out.println(cont);
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
}
-----------------------------------------------------------------------
Prueba2.java --->
------------------------------------------------------------------------
import java.io.*;
class Prueba2 {
public static void main(String[] args) {
try {
File fich = new File("escrito.txt");
FileInputStream sud = new FileInputStream(fich);
int cont;
int c;
cont = 0;
boolean sigue = true;
String tex = new String("");
bucle1:
while ((c = escrito.read()) != -1) {
if (c == (int)'$') {
sigue = false;
}
else {
sigue = true;
}
if (sigue) {
tex = tex + (char)c;
cont++;
}
}
System.out.println(tex);
System.out.println(cont);
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
}
-----------------------------------------------------------------------
Muchas gracias de antemano y perdón por el tostón.
Utiliza la clase java.io.StreamTokenizer que puedes construir con tu FileInputStream (aunque yo te recomendarÃa utilizar un Reader, que lee directamente en caracteres Unicode y te ahorras problemas), y pasarÃa el carácter a partir del cual no quieres que se siga leyendo más al método:
commentChar
public void commentChar(int ch)Specified that the character argument starts a single-line comment. All characters from the comment character to the end of the line are ignored by this stream tokenizer.
Any other attribute settings for the specified character are cleared.
Parameters:
ch - the character.
Esto hace exactamente lo que tu quieres de forma automática. Espero que te sirva.
commentChar
public void commentChar(int ch)Specified that the character argument starts a single-line comment. All characters from the comment character to the end of the line are ignored by this stream tokenizer.
Any other attribute settings for the specified character are cleared.
Parameters:
ch - the character.
Esto hace exactamente lo que tu quieres de forma automática. Espero que te sirva.
