Estoy practicando los ficheros de Java en netbeans y tenía una duda que no logro solucionar:
public class Ficheros {
public static void main(String[] args) throws FileNotFoundException {
Ficheros m = new Ficheros();
Scanner sc = new Scanner(System.in);
File file = new File("Prova.txt");
m.introduir();
int eleccio = sc.nextInt();
while (eleccio != 4) {
if (eleccio == 1) {
m.fichero(file);
}
if (eleccio == 2) {
m.lectura(file);
}
eleccio++;
}
}
public int introduir() {
System.out.println("Elegir opcion");
System.out.println("Verificar el fichero");
System.out.println("Leer el fichero");
return 0;
}
public void fichero(File file) {
if (file.exists()) {
System.out.println("Existe");
} else {
System.out.println("It doesn't");
}
}
public void lectura(File file) throws FileNotFoundException {
Scanner entrada = new Scanner(file);
System.out.println("");
while (entrada.hasNextLine()) {
System.out.println(entrada.nextLine());
}
entrada.close();
}
}
Cuando elijo el 1
, me muestra tanto el resultado del primer método como del segundo, haciendo un efecto cascada. Cosa que no pasa con la segunda.
Alguna solución?