开发者

File reference, what is the correct 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜