How to use file class in Java?

Java 8Object Oriented ProgrammingProgramming

Java File class represents the files and directory pathnames in an abstract manner. This category is employed for the creation of files and directories, file looking out, file deletion, etc. The File object represents the particular file/directory on the disk.

Example

Live Demo

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      File f = null;
      String[] strs = {"test1.txt", "test2.txt"};
 
      try {
         for(String s:strs ) {
            f = new File(s);
            boolean bool = f.canExecute();
            String a = f.getAbsolutePath();
            System.out.print(a);
            System.out.println(" is executable: "+ bool);
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

/home/cg/root/2880380/test1.txt is executable: false
/home/cg/root/2880380/test2.txt is executable: false
raja
Published on 12-Jan-2018 07:02:27
Advertisements