首页 > 解决方案 > 尝试在 java 中读取文本文件 - 错误:目录文件不存在 - LINUX

问题描述

我刚刚学习 java,在我目前的课程中学习如何阅读文本文件。我做了老师所做的一切,但是当我尝试运行它时出现错误:目录文件不存在。(错误:/home/jean/Documentos/Livros.txt(Arquivo ou diretório inexistente))

我正在使用linux,我做错了什么?

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class LeArquivos {

    public static void main(String[] args) {

        File file = new File("/home/jean/Documentos/Livros.txt");
        Scanner scan = null;

        try {
            scan = new Scanner(file);
            while(scan.hasNextLine()) {
                System.out.println(scan.hasNextLine());
            }`enter code here`
        }
        catch(IOException e) {
            System.out.println("ERROR: " + e.getMessage());
        }
        finally {
            if(scan != null) {
            scan.close();
            }   
    }
    }
}

标签: javafile

解决方案


如何检查目录的一部分。

Step 1 Open command line (windows + R type CMD) enter.
Step 2 Type cd /
Step 3 Type cd /home/jean/Documentos --> enter --> If command error part is incorrect.
Step 4 Type ls --> Fine file Livros.txt

如果文件找到的部分是正确的。

如果文件未找到部分不正确。

:::::::::更新::::::::

尝试是代码。

public static void main(String[] args) {

    File file = new File("/home/jean/Documentos/Livros.txt");
    BufferedReader br = null;

    try {

        br = new BufferedReader(new FileReader(file));

        String st;
        while ((st = br.readLine()) != null) {
            System.out.println(st);
        }
        br.close();
    } catch (IOException e) {
        System.out.println("ERROR: " + e.getMessage());
    }

}

跳是帮助。


推荐阅读