Google+

257. Files








Computer Files can be represented in Java Programming using File Class.  We need to create an object of File Class by providing the computer file path as shown below -

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

Using the above statement we are just representing the file that is supposed to be there at the provided computer file path. This statement wont throw any errors irrespective of whether the file is there or not at the provided location.

Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Project 'Project 50' as shown below -



2. Create a Java Class 'FilesDemo.java' with main( ) method as shown below -



3. Represent a file in Java Programming using File Class by providing your computer file path, irrespective of whether it is available or not available on your computer as shown below -


4. View the error message and select 'Import File (java.io) ' option as shown below -


5. Observe that the import statement got added and the issue got resolved as shown below -



Observe that the File Class Statement representing a file is not throwing any errors, even though we have not created any file at that location.

6. Go to the file path that is provided in the File Class object creation statement as shown below and check whether we have any file at that location -




Hence the statement File object = new File("File Path"); is used to represent a file in the form of object in Java Programming, irrespective of whether the file is available or not available at that specified location.






Please comment below to feedback or ask questions.

Creating a file using createNewFile( ) method will be explained in the next post.




No comments:

Blog Index