SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Puspender Tanwar wrote:
why
this code giving NoSuchFileException at //line1
as per the get(String first, String... more), i have just created an instance of Path. why that instance is not valid in this createFile method which takes path instance as argument?
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Partheban Udayakumar wrote:Why because you haven't specified a file name. Take a look at your above code correctly.
ankit garg wrote: Does the parent directory exist where you are trying to create the file?
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:If c:/myprograms/io/saveio2/A exists it will not give you an exception and the program will create a file named B at that location.
Puspender Tanwar wrote:correcting that code..
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Partheban Udayakumar wrote:To create a file, the last name should be the file's name like a.txt and if you want to create a folder, use Paths.createFolder() method.
Partheban Udayakumar wrote:
Puspender Tanwar wrote:
Still this is not correct,
As Ankit said, /A and /B wont considered as files. That is the reason you are getting an exception. If you want to create a file named B, you should give as ankit has given. To create a file, the last name should be the file's name like a.txt or if you want to create a folder, use Paths.createFolder() method.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Puspender Tanwar wrote:i don't thing Paths class is having any such static method createFolder(). neither File or Files are having such method. yes Files.createDirectory() is there for such operation
Ankit Garg wrote:The reason is as you said Partheban, the createFile method is supposed to create a file, not the parent directory. The exception is a bit misleading, instead of NoSuchFileException it should throw some "NoSuchParentDirectoryException"
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Partheban Udayakumar wrote:
Ankit Garg wrote:The reason is as you said Partheban, the createFile method is supposed to create a file, not the parent directory. The exception is a bit misleading, instead of NoSuchFileException it should throw some "NoSuchParentDirectoryException"
I agree with you. It can be no such directory exception but you forgot that Java runs on a machine, which unlike human, doesn't have reasoning capacity. So for it, if the developers have fed c:/myprograms/io/saveio2/A/B as file, it is a file. So it would throw only a NoSuchFileException. Since you are a human, you should have checked the full path.
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:The reason I say it should throw some better exception is because a method called "createFile" throwing "NoSuchFileException" is confusing. The method name suggests it will create a file and the exception says there is no such file. So the developer might say "I know there is no such file, that's why I've called this method, create the file for me"
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
A "dutch baby" is not a baby. But this tiny ad is baby sized:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|