繁体   English   中英

Java:BufferedReader 未打印出 TXT 文件上的文件内容

[英]Java: BufferedReader not printing out File content on TXT file

我在使用 BufferedReader 读取文件夹中 txt 文件的内容时遇到问题,该文件夹通过方法showEditFile()调用,该方法使用带有来自方法pideNumero.preguntaUno(); 它需要一个 int 来遍历数组位置:

遍历文件夹“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;

    }

读取内容的第一行的方法应该是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());
        }
    }

我试图在调试模式下检查并在线查看FileReader fr = new FileReader(document); 它会使用 FilePath == null直接跳入 FileNotFoundException,我认为这是问题所在。

似乎它不知道“Archivos”之后的路径

路径:根\Archivos\kiki.txt

我已经坚持了一整天了,有人可以帮忙吗!

carpeta.list()没有给出完全限定的路径。 它只给你文件名。 所以下一次调用new File(archivos[menu - 1])将失败。 new File(archivos[menu - 1])中,您需要提供完整的,然后您将不会得到异常。 参考https://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()

所以感谢@Jags,我解决了这个问题。 解决方案如下:

在我的数组中,我返回了一个基本上保存字符串的 String[]...没有分配给它的文件路径,因为它只是一个字符串)。

我在这里所做的更改:

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;

    }

现在在我的另一个方法 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());
        }
    }

现在它打印出文件的第一行(在本例中是hello)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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