简体   繁体   English

Paths.get找不到Windows 10文件(Java)

[英]Paths.get doesnt find file Windows 10 (java)

I'm following some course on udemy . 我正在学习一些关于udemy的课程。 Im learning about Paths , but i cant get Paths.get to work. 我正在学习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();
      }
 }
 }

The File exists , the name is correct and its on the C drive. 文件存在,名称正确,并且在C驱动器上。 What am i doing wrong? 我究竟做错了什么?

java.nio.file.NoSuchFileException: C:\OutThere.txt

at com.bennydelathouwer.Main.main(Main.java:16)

It's a bad practice to hard-code "/" or "\\" use: 硬编码“ /”或“ \\”使用是一种不好的做法:

 File.separator

++ are you sure you have the proper privileges to read this file? ++您确定您具有读取此文件的适当权限吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM