开发者

File reference, what is the correct syntax

开发者 https://www.devze.com 2022-12-21 17:16 出处:网络
I am trying to refer to a location on my computer, however Java is telling me that I have the wrong syntax.

I am trying to refer to a location on my computer, however Java is telling me that I have the wrong syntax.

开发者_运维百科

Is this line correct?

File newlyUploadedFile = new File("D:\\" + fileName);

The thing is the file gets uploaded correctly to the location I want it to go to, but I get the error:

java.io.IOException: The filename, directory name, or volume label syntax is incorrect


Escape your backslashes in java strings, always.

File newlyUploadedFile = new File("D:\\" + fileName);

The IOException is caused by the system not finding the file you specified in filename. Try adding

newlyUploadedFile.exists();

and see what it returns. If your path returns false then you have a mistake in filename.


First off, the code as presented in the question will not compile. But since you have seen an IOException, you are clearly running different code.

In order to get an IOException complaining about pathname syntax, there must actually be something wrong with pathname. What does "D:\\" + fileName actually give you? Add a call to System.err.println(...) to see what it is.


Notes:

  1. That exception text is coming from the Windows operating system, not from Java.

  2. There is never any need to use backslashes in Java filenames. At least if there is I've never encountered one in 13 years.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号