• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with "File" constructor  RSS feed

 
Emilian Chmiel
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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




Why I must use "inFile.createNewFile();", why constructor dosen't create file ?
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Carey Brown
Bartender
Posts: 3016
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!