- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9313 Articles for Object Oriented Programming
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
3K+ Views
The method java.io.File.list() is used to obtain the list of the files and directories in the specified directory defined by its path name. This list of files is stored in a string array. If the length of this string array is greater than 0, then the specified directory is not empty. Otherwise, it is empty.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { File directory = new File("C:\JavaProgram"); if (directory.isDirectory()) { String[] files = directory.list(); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
314 Views
The method java.io.File.renameTo() is used to rename a file or directory. This method requires a single parameter i.e. the name that the file or directory is renamed to and it returns true on the success of the renaming or false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { try { File file1 = new File("demo1.txt"); File file2 = new File("demo2.txt"); file1.createNewFile(); file2.createNewFile(); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
752 Views
A file can be set to read-only by using the method java.io.File.setReadOnly(). This method requires no parameters and it returns true if the file is set to read-only and false otherwise. The method java.io.File.canWrite() is used to check whether the file can be written to in Java and if not, then the file is confirmed to be read-only.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { boolean flag; try { File file = new File("demo1.txt"); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
234 Views
A file or directory can be deleted on termination of the program i.e. after the virtual machine terminates using the method java.io.File.deleteOnExit(). This method requires no parameters and it does not return a value.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("File: " + file); file.deleteOnExit(); } catch(Exception e) { e.printStackTrace(); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
154 Views
The contents of a directory can be obtained using the method java.io.File.listFiles(). This method requires no parameters and it returns the abstract path names that specify the files and directories in the required directory.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo { public static void main(String[] args) { File directory = new File("C:\JavaProgram"); File[] contents = directory.listFiles(); for (File c : contents) { if(c.isFile()) System.out.println(c + " is a file"); ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
245 Views
The name of the specified file or directory can be obtained using the method java.io.File.getName(). The getName() returns the name of the file or the directory and it requires no parameters.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { File file = new File("C:" + File.separator + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File name: " + file.getName()); } }The output of the above program is as follows −OutputFile name: demo1.txtNow let us understand the above program.The name of the specified ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
208 Views
The name of the parent directory of the file or directory can be obtained using the method java.io.File.getParent(). This method returns the parent directory pathname string or null if there is no parent named.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { File file = new File("C:" + File.separator + "JavaProgram" + File.separator, "demo1.txt"); System.out.println("File: " + file); System.out.println("Parent: " + file.getParent()); } }The output of the above program is as follows −OutputFile: C:\JavaProgram\demo1.txt Parent: C:\JavaProgramNow ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
2K+ Views
The method java.io.File.canRead() is used to check if a file or directory is readable in Java. This method returns true if the file specified by the abstract path name can be read by an application and false otherwise.A program that demonstrates this is given as follows −Example Live Demoimport java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("The file can be read? " + file.canRead()); } catch(Exception e) { ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
6K+ Views
The method java.io.File.exists() is used to check whether a file or a directory exists or not. This method returns true if the file or directory specified by the abstract path name exists and false if it does not exist.A program that demonstrates this is given as follows −Exampleimport java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("c:/JavaProgram/demo1.txt"); file.createNewFile(); System.out.println(file.exists()); } catch(Exception e) { e.printStackTrace(); } ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
388 Views
The method java.io.File.isFile() is used to check whether the given file is an existing file in Java. Similarly, the method java.io.File.isDirectory() is used to check whether the given file is a directory in Java. Both of these methods require no parameters.A program that demonstrates this is given as follows −Examplepublic class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); boolean fileFlag = file.isFile(); if (fileFlag) { ... Read More