Java:BufferedReader在不打印TXT文件上的文件内容

huangapple go评论63阅读模式
英文:

Java: BufferedReader not printing out File content on TXT file

问题

我在使用BufferedReader读取txt文件夹中的内容时遇到问题该文件夹通过方法`showEditFile()`调用方法中使用了来自方法`pideNumero.preguntaUno();`的用户输入数组来调用该方法接受一个整数来迭代数组位置

遍历文件夹"Archivos"的数组

```java
    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);这一行,它会直接跳转到FileNotFoundException,FilePath为null,我认为问题是从这里引起的。

似乎它不知道"Archivos"之后的路径。

路径:
Root\Archivos\kiki.txt

我已经卡在这里整整一天了,有人可以帮忙吗!


<details>
<summary>英文:</summary>

I&#39;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 &quot;Archivos&quot;. 

public static String[] testFiles() {

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

    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(&quot;Por Favor, elige un archivo con su numero para mostrar su contenido &quot;);
    System.out.println(&quot;Los archivos dentro la carpeta Archivos son: &quot;);
    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(&quot;File not found.&quot; + document.toString());
    } catch (IOException e) {
        System.out.println(&quot;Unable to read file: &quot; + 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&#39;t know the path after &quot;Archivos&quot; 

Path:
Root\Archivos\kiki.txt

**I&#39;ve been stuck on this for a full day now can someone please help!**






</details>


# 答案1
**得分**: 1

```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()。

<details>
<summary>英文:</summary>

```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()



</details>



# 答案2
**得分**: 0

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

在我的数组中,我返回了一个String[],它基本上保存了字符串... 因此,当我尝试从另一个方法调用它以读取内容时,它会显示文件的名称(它是一个字符串,但是不会分配文件路径,因为它只是一个字符串)。

我在这里做出的更改:

```java
public static File[] testFiles() {

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

       File carpeta = new File(folderPath);
       // 正如你所看到的,我在这里使用了listFiles()方法来列出所有文件并将它们保存到File[]数组中
       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")。

英文:

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(&quot;user.dir&quot;);
        String separator = File.separator;
        String folderPath = endPath + separator + &quot;Archivos&quot;;

        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(&quot;Por Favor, elige un archivo con su numero para mostrar su contenido &quot;);
        System.out.println(&quot;Los archivos dentro la carpeta Archivos son: &quot;);
        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(&quot;File not found.&quot; + document.toString());
        } catch (IOException e) {
            System.out.println(&quot;Unable to read file: &quot; + document.toString());
        }
    }

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

huangapple
  • 本文由 发表于 2020年4月10日 01:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61127206.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定