Program to Identify File or Directory - in java file IO | And in java 7 using java.nio.file

In this post we’ll write program to Identify File or Directory - in java file IO |  in java 7 using java.nio.file

  • Program 1 to Identify File or Directory - in java file IO
  • Program 2 to Identify Directory or not - in java 7 using java.nio.file.


Program 1 to Identify File or Directory - in java file IO >
java.io.File's isFile() method can be used to find it's File or not
Method returns true  - if it's a File.
Method returns false - if it's not a File.

File's isDirectory() method can be used to find it's Directory or not
Method returns true  - if it's a Directory.
Method returns false - if it's not a Directory.

import java.io.File;
/** JavaMadeSoEasy.com */
public class FileOrDirectory {
   public static void main(String... args) {
          String fileName = "c:/myFile.txt";
          File fileOrDir = new File(fileName);
          //Is a File ?
          if(fileOrDir.isFile())
                 System.out.println(fileName +" is a File");
          else
                 System.out.println(fileName +" isn't a File");
          //Is a Directory ?
          if(fileOrDir.isDirectory())
                 System.out.println(fileName +" is a Directory");
          else
                 System.out.println(fileName +" isn't a Directory");
         
                
         
   }
}
/* OUTPUT
c:/myFile.txt is a File
c:/myFile.txt isn't a Directory
*/




Must read Directory operations - create, delete, copy, traverse.

Program 2 to Identify Directory or not - in java 7 using java.nio.file >
java.nio.file.attribute.BasicFileAttributes's isDirectory() method can be used to find its Directory or not
Method returns true  - if it's a Directory.
Method returns false - if it's not a Directory.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
/** JavaMadeSoEasy.com */
public class FileOrDirectory {
   public static void main(String... args) throws IOException {
          String fileName = "c:/myDirectory";
          // create java.nio.file.Path
          Path path = Paths.get(fileName);
          // create BasicFileAttributeView and BasicFileAttributes
          BasicFileAttributeView basicFileAttributeView = Files
                       .getFileAttributeView(path, BasicFileAttributeView.class);
          BasicFileAttributes basicFileAttributes = basicFileAttributeView
                       .readAttributes();
          //Is a Directory ?
          if(basicFileAttributes.isDirectory())
                 System.out.println(fileName +" is a Directory");
          else
                 System.out.println(fileName +" isn't a Directory");
  
  
         
   }
}
/* OUTPUT
c:/myDirectory is a Directory
*/


RELATED LINKS>

Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


2 Program to Delete a Directory


Copy a Directory


Traverse a Directory (all sub-directories and files) in 2 ways


Program to Write BYTE data type in file using DataOutputStream and Read BYTE from file using DataInputStream - java file IO

Create File using createNewFile() method in java file IO



eEdit
Must read for you :