258. Creating a file using createNewFile( ) method

Pre-requisite -

We can create a file using createNewFile( ) method. 

Example -

File object = new File("C://Users//Arun//Desktop//Files//abcd.txt");

object.createNewFile( ); //Creates the File at the above specified file path

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, open the '' Class in the existing Java Project 'Project 50' as shown below -

2. Now lets create a File at the specified file path location of the File Class object creation statement using the createNewFile( ) method as shown below -

3. View the compiler exception error message and select the 'Surround with try/catch' option from the error message as shown below -

4. Observe that the file creation statement got surrounded by the try catch statements and the error got resolved as shown below -

5. Go to the file path location specified in the File Class object creation statement and find that there are no files at that location before executing our Java program as shown below -

6. Save & Run the Java class '' , again go to the same file path location and observe that a file with the file name that is specified in the File object creation statement got created as shown below -

Hence we have created a file using the createNewFile( ) method at the specified file path location in the File Class object creation statement.

Please comment below to feedback or ask questions.

Creating files in your Project Workspace  will be explained in the next post.

No comments: