java里文件名语法,为什么在Java中出现FileNotFoundException(文件名,目录名或卷标签语法不正确)?...

博主分享了一个Java程序中尝试打开并写入文件时遇到的FileNotFoundException,原因在于路径字符串中包含非打印字符。通过测试代码发现,问题出在复制粘贴过程中引入的额外字符。解决方法是确保路径字符串正确无误。

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());

The output is

yours len=49, retype=48

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值