java - 尝试在 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();
}
}
}
}
解决方案
如何检查目录的一部分。
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());
}
}
跳是帮助。
推荐阅读
- vue.js - 如何在 nuxtjs 中的每个页面更改上运行脚本
- r - 如何在 R 中对直方图数据帧进行 ANOVA 和 Tukey 的 HSD
- c# - 为什么保存文本的淡入/淡出在保存时只淡出一次?
- javascript - Barba.js (v2.9.7) 重新加载当前页面而不是下一个
- python - Python 子类重写了具有不同签名的方法,因此不调用基类方法。这是为什么?
- authentication - Auth0 身份验证和 Google 日历请求流程
- maven - Maven 不在线搜索依赖项(intelij)
- angularjs - 角度过滤器功能
- python - 只知道开始和结束字符时收集列表中的子字符串
- javascript - Javascript 在不同的上下文中访问“文档”对象(反应)