• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Files.createFile method throwing NoSuchFileException

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code runs perfectly

then 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?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the parent directory exist where you are trying to create the file?
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



Why because you haven't specified a file name. Take a look at your above code correctly.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:Why because you haven't specified a file name. Take a look at your above code correctly.


that's a mistake made by me here only. But according to you if the wrong statement is this, then would give compilation error not an exception
correcting that code..


ankit garg wrote: Does the parent directory exist where you are trying to create the file?


c:/myprograms/io/saveio2 upto this the directory exist .
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. Also you should use the additional parameters as:
Paths.get("c:/myprograms/io/saveio2","A", "B");
This won't change the output of the program, but I guess from the API point of view this way is better as the API adds path separators...
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


it works.. thank you
but ankit, why is it so ?
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Puspender Tanwar wrote:correcting that code..



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.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


that's incorrect, no need to write extensions (.txt, .jpg), if you don't write a extension then also a file is created

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
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


Well I tried that and even with /A and /B it works as long as the path till /A exists.

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" , or at least the message should be correct, the exception says:

NoSuchFileException: c:/myprograms/io/saveio2/A/B
But it should say:
NoSuchFileException: c:/myprograms/io/saveio2/A

So that you know that the problem is with the parent folder not existing...
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Pardon me for that. It is createDirectory. I was in a hurry, so I typed createFolder.

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.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


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"
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"


I totally agree with you
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic