Why am I getting a FileNotFoundException (The filename, directory name, or volume label syntax is incorrect) in Java? [closed]

限于喜欢 提交于 2019-12-13 08:58:48

问题


I am trying to create a program where it takes simple input and writes it to a file. Problem is, when it tries to open the file to write to it, I get the error: "java.io.FileNotFoundException: ‪C:\Users\bobdu\eclipse-workspace\SHIPTesting.txt (The filename, directory name, or volume label syntax is incorrect)." I even have a very simple program where I get the same error:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class OutputTesting {
    public static void main(String[] args)
    {
     try
     {
        PrintWriter outputStream = new PrintWriter(new FileOutputStream("‪C:\\Users\\bobdu\\eclipse-workspace\\SHIPTesting.txt"));
        outputStream.println("Output line 1");
        outputStream.println("Output line 2");
        outputStream.close();
    }
    catch (FileNotFoundException e)
    {
        System.err.println(e.getMessage());
        e.printStackTrace(System.err);
        System.exit(0);
    }
 }

}

The file does exist for sure, I can find it in my directory. Thank you in advance for helping me.


回答1:


You have a bad character in your path. When I try to paste it into eclipse, I get:




回答2:


You have an extra non-printable character in your path string. It survived the copy paste as well, so i was able to reproduce your error. Here is a test:

    String yours = "‪C:\\Users\\bobdu\\eclipse-workspace\\SHIPTesting.txt";
    String retyp = "C:\\Users\\bobdu\\eclipse-workspace\\SHIPTesting.txt";

    System.out.println("yours len="+yours.length()+", retype=" + retyp.length());

The output is

yours len=49, retype=48


来源:https://stackoverflow.com/questions/51713047/why-am-i-getting-a-filenotfoundexception-the-filename-directory-name-or-volum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!