java - 尝试用 java 读取文本文件 - 错误 : File of directory does not exist - LINUX

标签 java file

我刚刚学习 java,在当前的类(class)中我正在学习如何读取文本文件。我做了老师所做的一切,但是当我尝试运行它时,我收到错误:目录文件不存在。 (错误:/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();
            }   
    }
    }
}

最佳答案

如何检查部分目录。

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());
    }

}

跳跃是有帮助的。

关于java - 尝试用 java 读取文本文件 - 错误 : File of directory does not exist - LINUX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60646730/

相关文章:

java - java在指定目录动态创建文本文件,写入数据,从该文件读取数据

java - REST 确保异步 - 找不到方法

java - 如何使用 Java 获取注解名称

java - 使用 Java8 流将 Object 缩减为 Map

java - Spark将多行转换为具有多个集合的单行

java - PrimeFaces OneRadio + "spacebar" key

java - 打开一个二进制文件并打印该文件中的所有 ASCII 字符

javascript - Node.js 强制等待函数完成

java - getFilesDir() 导致空指针异常

file - 如何使用远程系统上的 Ansible 任务 move/重命名文件