我刚刚学习 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/