为什么在Java中出现FileNotFoundException(文件名,目录名或卷标签语法不正确)? [英] Why am I getting a FileNotFoundException (The filename, directory name, or volume label syntax is incorrect) in Java?
问题描述
我正在尝试创建一个程序,在此程序中,它需要简单的输入并将其写入文件.问题是,当它尝试打开文件写入文件时,出现错误:"java.io.FileNotFoundException:C:\ Users \ bobdu \ eclipse-workspace \ SHIPTesting.txt(文件名,目录名或卷标签语法不正确)."我什至有一个非常简单的程序,在其中遇到了相同的错误:
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.
推荐答案
您的路径字符串中还有一个额外的不可打印字符.它在复制粘贴中也幸免于难,所以我能够重现您的错误.这是一个测试:
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());
输出为
yours len=49, retype=48
这篇关于为什么在Java中出现FileNotFoundException(文件名,目录名或卷标签语法不正确)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!