I'm trying to print a .txt on the screen.I have a menu with different options and I don't know why it fails. The file directory should be defined by the string FITXER. I only want to print the text file, I don't need to save the content. The bug is on the BufferedReader
and FileReader arxiu
, it's the name of the file variable that gets the value of FITXER.
import java.util.Scanner;
import java.io.*;
public class Copiador {
Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws IOException {
Copiador programa = new Copiador();
programa.inici();
}
public void inici() throws IOException {
int opcio;
do {
System.out.println("Llegir (1)");
System.out.println("Copiar (2)");
System.out.println("Surtir (3)");
opcio = sc.nextInt();
switch (opcio) {
case 1:
Llegir();
break;
case 2:
System.out.println("Skipped Consultar");
//Consultar(nomusuaris);
break;
}
} while (opcio != 3);
sc.close();
}
public void Llegir() throws IOException {
String FITXER;
File arxiu = null;
FileReader fr = null;
BufferedReader br = null;
System.out.println("Que vols llegir?");
FITXER = sc.nextLine();
arxiu = new File(FITXER);
fr = new FileReader(arxiu);
br = new BufferedReader(fr);
String linea;
while ((linea = br.readLine()) != null)
System.out.println(linea);
System.out.println("Confirmat");
fr.close();
}
}