Found 9313 Articles for Object Oriented Programming

Check if a directory is not empty in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

Rename file or directory in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

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

Mark file or directory Read Only in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

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

Delete file or directory on termination in Java

Samual Sam
Updated on 30-Jul-2019 22:30:24

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

Java Program to get the content of a directory

Samual Sam
Updated on 30-Jul-2019 22:30:24

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

Java Program to get name of specified file or directory

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

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

Java Program to get name of parent directory

Samual Sam
Updated on 30-Jul-2019 22:30:24

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

Java Program to check if a file or directory is readable

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

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

Determine if file or directory exists in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

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

Check for file or directory in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:24

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

Advertisements