简体   繁体   中英

Java: BufferedReader not printing out File content on TXT file

I'm having issues with BufferedReader reading out the content of a txt file within a folder which is called via a method showEditFile() using an array with input of the user from method pideNumero.preguntaUno(); which takes an int to iterate through the array positions:

Array that loops through the Folder "Archivos".

    public static String[] testFiles() {

        String endPath = System.getProperty("user.dir");
        String separator = File.separator;
        String folderPath = endPath + separator + "Archivos";

        File carpeta = new File(folderPath);

        String[] lista = carpeta.list();

        return lista;

    }

Method which would read the first line of the content which should be Hellowwwww :

 public static void showEditFile() throws IOException {

        System.out.println("Por Favor, elige un archivo con su numero para mostrar su contenido ");
        System.out.println("Los archivos dentro la carpeta Archivos son: ");
        Menu.listFiles.nomFiles();

        String[] archivos = Menu.listFiles.testFiles();

        int menu = Menu.pideNumero.preguntaUno();

        File document = new File(archivos[menu - 1]);

        try {
            FileReader fr = new FileReader(document);
            BufferedReader br = new BufferedReader(fr);
            String line;
            line = br.readLine();

            System.out.println(line);

        } catch (FileNotFoundException e) {
            System.out.println("File not found." + document.toString());
        } catch (IOException e) {
            System.out.println("Unable to read file: " + document.toString());
        }
    }

I tried to check in Debug mode an see that on line FileReader fr = new FileReader(document); it would jump straight into the FileNotFoundException with the FilePath == null which I think comes the problem from.

It seems it doesn't know the path after "Archivos"

Path: Root\Archivos\kiki.txt

I've been stuck on this for a full day now can someone please help!

carpeta.list() does not give fully qualified path. It only gives you file name. So next call new File(archivos[menu - 1]) will fail. In new File(archivos[menu - 1]) you will need to provide full and then you will not get Exception. Refer to https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()

So thanks to @Jags I figured the issue out. Solution below:

In my array I was returning a String[] which saves basically strings... So when I try to call it from my other method to read out the content it would show me the name of the file (which is a string but wouldn't have a file path assigned to it because it's just a string).

Changes I made here:

public static File[] testFiles() {

        String endPath = System.getProperty("user.dir");
        String separator = File.separator;
        String folderPath = endPath + separator + "Archivos";

        File carpeta = new File(folderPath);
        // here as you can see i used the listFiles() method to list all the files and 
        // and save them into the File[] array
        File[] lista = carpeta.listFiles();   

        return lista;

    }

Now when calling the method in my other method showEditFiles():

public static void showEditFile() throws IOException {

        System.out.println("Por Favor, elige un archivo con su numero para mostrar su contenido ");
        System.out.println("Los archivos dentro la carpeta Archivos son: ");
        Menu.listFiles.nomFiles();

        File[] archivos = Menu.listFiles.testFiles();

        int menu = Menu.pideNumero.preguntaUno();

        File document = new File(archivos[menu - 1]);

        try {
            FileReader fr = new FileReader(document);
            BufferedReader br = new BufferedReader(fr);
            String line;
            line = br.readLine();

            System.out.println(line);

        } catch (FileNotFoundException e) {
            System.out.println("File not found." + document.toString());
        } catch (IOException e) {
            System.out.println("Unable to read file: " + document.toString());
        }
    }

Now it prints out the first line of the file (which in this case was hellow).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM