1

我正在学习一些关于 udemy 的课程。我正在学习 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)
4

1 回答 1

0

硬编码“/”或“\”使用是一种不好的做法:

 File.separator

++ 你确定你有适当的权限来读取这个文件吗?

于 2018-09-18T15:01:19.087 回答