Forums Register Login

Problem with "File" constructor

+Pie Number of slices to send: Send


Exception in thread "main" java.io.FileNotFoundException: C:\in.txt ...




Why I must use "inFile.createNewFile();", why constructor dosen't create file ?
+Pie Number of slices to send: Send
It doesn't make sense to create a new file if all you want to do is read it.

File was named poorly. A File does not represent a file on your filesystem. A File represents a path, and the actual file it points to may or may not exist.

You shouldn't use the File class. Instead, use Paths, Path, and Files to open streams and readers:

+Pie Number of slices to send: Send
From the Javadocs: An abstract representation of file and directory pathnames.

As such, a File does not know if it refers to an actual storage location until it is asked to perform some operation, e.g. calling exists().

In your example code, creating a file and the opening it up for read doesn't make sense, there's nothing to read immediately after creating it.
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 327 times.
Similar Threads
Annonymous classes
try without catch
files
File Not Found Error
Externalizable interface
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:00:16.