문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top