Apache Commons IO - FileEntry Class



Overview

FileEntry class provides the state of a file or directory. File attributes at a point in time.

Class Declaration

Following is the declaration for org.apache.commons.io.monitor.FileEntry Class −

public class FileEntry
   extends Object implements Serializable

Features of FileEntry

FileEntry class object provides the following file attributes at a point in time.

  • getName() − file name.

  • exists() − checks if file exists or not.

  • isDirectory() − checks if file is a directory.

  • lastModified() − gives last modified date time.

  • listFiles() − gives content of directory.

Here is the input file we need to parse −

Welcome to TutorialsPoint. Simply Easy Learning.

Example - Get/Set Existing File Check

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileEntry;

public class CommonsIoTester {
   public static void main(String[] args) {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      FileEntry fileEntry = new FileEntry(file);
      fileEntry.setExists(true);
      System.out.println("File Exists: " + fileEntry.isExists());
   }
}

Output

It will print the following result −

File Exists: true

Example - Get File Attributes

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileEntry;

public class CommonsIoTester {
   public static void main(String[] args) {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      FileEntry fileEntry = new FileEntry(file);
      System.out.println("Monitored File: " + fileEntry.getFile());
      System.out.println("File name: " + fileEntry.getName());
   }
}

Output

It will print the following result −

Monitored File: input.txt
File name: input.txt

Example - Get/Set directory status of a File

CommonsIoTester.java

package com.tutorialspoint;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.monitor.FileEntry;

public class CommonsIoTester {
   public static void main(String[] args) {
      //get the file object
      File file = FileUtils.getFile("input.txt");
      FileEntry fileEntry = new FileEntry(file);
      fileEntry.setDirectory(true);
      System.out.println("Is Directory: " + fileEntry.isDirectory());
   }
}

Output

It will print the following result −

Is Directory: false
Advertisements