具有绝对路径的Java FileNotFoundException-无法读取或执行,但是文件存在 [英] Java FileNotFoundException with Absolute path - Cant Read or Execute, but file exists

查看:371
本文介绍了具有绝对路径的Java FileNotFoundException-无法读取或执行,但是文件存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定已经解决了这个问题,但是十个不同的策略都无法解决这个问题.

I'm sure this has been answered, but ten different strategies hasn't worked on this issue.

如果我使用C:\ Users \ Anny \ Dropbox \ SocialMediaOcto \ instructions \ Trees \ instructions.txt 作为文件的绝对路径,IDEA无法从该路径读取或执行.如果我使用相同的路径并将其粘贴到Windows资源管理器中,它将立即执行.我不想专注于工作目录,因为此文件可以用作程序的配置文件,但是用反斜杠替换斜杠不起作用,绝对路径仍将我带到该文件,但是IDEA无法启动.

If I use C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt as my absolute path for the file, IDEA cannot read or execute from this path. If I take that same path and paste it into windows explorer, it will execute right away. I dont want to focus on a working directory as this file works as the program's configurations file, but replaceing the slashes with backslashes doesnt work, the absolute path still brings me to the file, but IDEA doesnt launch.

我机智.

 public static String generateFileName(String folder){

    String filename = "";
    List<String> hashtags = new ArrayList<>();
    String instructions_file =         "C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt";

    //does not return true-true, but can launch file on windows explorer..
    System.out.println("FILE EXIST AND EXECUTE?" + new File(instructions_file).getAbsoluteFile().canRead() +" "+new File(instructions_file).getAbsoluteFile().canExecute());

    System.out.println(new File(instructions_file).getAbsoluteFile());
    //C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt

    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(new File(instructions_file).getAbsoluteFile()));

编辑 用反斜杠替换反斜杠后,阅读器仍然无法正确读取或执行文件.

EDIT After replacing backslashes with forward slashes, the reader still cannot properly read or execute the file.

日志: 字符串显示: C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Bees/instructions.txt

LOG: The string prints: C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Bees/instructions.txt

  java.io.FileNotFoundException:    C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Bees\instructions.txt (The system cannot find the file specified)

推荐答案

正确的网址:

String instructions_file = "C:/Users/Anny/Dropbox/SocialMediaOcto/instructions/Trees/instructions.txt";

因为\是Java中的转义字符.如果要使用\作为字符,则必须自己对其进行转义.

Because \ is an escape character in Java. If you want to use \ as a character you have to escape it itself.

更正网址v2:

String instructions_file  = "C:\\Users\\Anny\\Dropbox\\SocialMediaOcto\\instructions\\Trees\\instructions.txt";

你所拥有的:

String instructions_file  = "C:\Users\Anny\Dropbox\SocialMediaOcto\instructions\Trees\instructions.txt";

由Java读取为

"C:{something}sers{something}nny{something}ropbox{something}ocialMediaOcto{something}nstructions\Trees\instructions.txt"

我认为最好使用第一种方法,因为它是平台安全的.

In my oppinion it's much better to use the first approach as it's platform safe.

这篇关于具有绝对路径的Java FileNotFoundException-无法读取或执行,但是文件存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆