java2db

How to create a file from createNewFile method in java



It is easy to create a file from createNewFile method in java.io.File . This class is providing four constructors. Use any one of the constructor with appropriate arguments to create a File.

Points to remember:
Specify correct path name(location) and file type(ie .txt,.pdf...) at the time of File instantiation.
new File(URI),here URI is system-dependent, hence the transformation performed by this constructor is also system-dependent, pass formatted URI to File constructor
Returns true if the file creation is success, return false when the file is already exists in the specified path or fail to create

Syntax:

<fileObjectName>.createNewFile();

Example Code:

Execution Result:

Conclusion :

Catch the URISyntaxException when creating the file with (new File(URI uri)) URI argumented Constructor.
If we given only the file name (ie new File(“newFile.txt”)) rather than path or URI ,the file will create in the current directory.
click to go BACK




Related Posts :
Write String to file using FileWriter in java
Get or Set Last Modified Date and Time for a File or directory in java
   [will not be published]



^ <