问题
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