Google+

259. Creating files in your Project Workspace








We create files while working on the Project for Project purpose only. Hence we have to learn how to create files in our project workspace, instead of creating them at different locations on our computer.

To start with, first lets find out our project workspace on our computer by following the below steps in Eclipse IDE -

1. Launch Eclipse IDE, Right click on our existing Java Project 'Project 50' and select 'Properties' option as shown below -


2. In 'Properties' dialog, view our Projects Workspace path on our computer as shown below -


3. Go to the above given path on our computer and observe that the following files & folders are displayed -


Now, you know how to go to your Projects workspace path.

In order to create a file say 'xyz.txt'  in our workspace using createNewFile( ) method, we have to specify the file name directly in File Class object creation statement, instead of giving the complete file path as shown below -

File obj = new File("xyz.txt");
obj.createNewFile( );

Lets implement this on Eclipse IDE -

1. Create a new Java Class 'CreateFilesDemo.java' with main( ) method as shown below -


2. Create a File Class object 'file1'  by specifying the File Name say 'xyz.txt' in the path as shown below (Resolve any import errors if any)  -


3. Create a file using the createNewFile( ) method as shown below  -


4. Resolve the error by selecting 'Surround with try/catch' option from the error message and observe that the error got resolved as shown below -


5. Save & Run the Java Class 'CreateFileDemo.java' and observe that the specified 'xyz.txt' file in the File Class file1 object creation statement, got created by our Java Program in our workspace as shown below -


Hence if we provide only the file name say 'xyz.txt' in our File Class object creation statement and use the createNewFile( ) with the created object to create the file, the specified file will be created directly under our Project Workspace as shown in the above screen.







Please comment below to feedback or ask questions.

Creating folders using mkdir( ) method will be explained in the next post.









No comments:

Followers :)

Blog Index