我正在学习 udemy 上的一些类(class)。我正在学习 Paths ,但我无法让 Paths.get 工作。
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
Path filePath = Paths.get("C:\\OutThere.txt");
printFile(filePath);
}
private static void printFile(Path path){
try(BufferedReader fileReader = Files.newBufferedReader(path)){
String line;
while((line = fileReader.readLine())!=null){
System.out.println(line);
}
}catch(IOException e){
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
文件存在,名称正确,位于C盘。我做错了什么?
java.nio.file.NoSuchFileException: C:\OutThere.txt
at com.bennydelathouwer.Main.main(Main.java:16)
最佳答案
对“/”或“\”使用进行硬编码是一种不好的做法:
File.separator
++ 您确定您有适当的权限来读取此文件吗?
关于java - Paths.get 找不到文件 Windows 10 (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52383676/