Core Java Interview Questions – File Handling
This Core Java Interview Questions – File Handling post providing all the interview questions and answers including the creation of files, the creation of directories, processing files such as renaming, deleting, setting file permissions and much more.
What is java.io.File class?
Used to perform operations on Files and Directories in java.
What is File?
A physical storage area by which we store a collection of bytes permanently.
What is Directory?
A folder that stores related information in the system, hence we can access easily at the later time.
How to create a File in Java?
File f = new File(“d:/sample.txt”);
How to create Directory using File class?
File d = new File(“d:/test”);
How to create Sub directories using File class?
File d = new File(“g:/dir1/dir2/dir3”);
How File instance represent both File & Directory ?
Files having extensions whereas Directories don’t have.
File f = new File(“file.txt”);
File d = new File(“directory”);
How to check whether the file is in Hidden mode or not?
Using isHidden() method.
How to set read-only permissions on file?
How to delete a file or directory using File class?
Using delete() function we can delete Files and Directories.