Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check any files exist, in a given folder

Tags:

java

I've updated my code to handle the listed scenario 1. Check for empty directory 2. Check if the file exist

What other things I should be looking out for when I'm trying to do a filewatch program?

I want to be able to create something that can cover all the possible scenario that could happen when monitoring the file such that when i deploy the code, I can quickly read the output and know what happened

package filemon;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * @author LAI XUEYANG
 * @version $Revision: 1.0 $
 */
public class filemon {

    /**
     * Method main.
     * 
     * @param args
     *            String[]
     */
    public static void main(String[] args) {

        // Declare yesterday date with format as yyyyMMdd
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        // Calendar yesterdayCal = Calendar.getInstance();

        // Declare today date with format as yyyyMMdd
        Calendar todayCal = Calendar.getInstance();
        String todayDate = dateFormat.format(todayCal.getTime());

        // Declaration of folder path
        File file = new File("D:\\TEST");

        // Declaration of file path
        Path filePath = Paths.get(file + "\\INCIF" + todayDate + ".txt");

        // yesterdayCal.add(Calendar.DATE, -1);
        // String yesterdayDate = dateFormat.format(yesterdayCal.getTime());

        try {
            checkEmptyDirectory(file, filePath);
        } catch (Exception e) {
            System.out.println(e);
        }
        // checkFileExist();
    }

    /**
     * Method checkFileExist.
     * 
     * @param filePath
     *            Path
     */
    private static void checkFileExist(Path filePath) {
        if (Files.exists(filePath)) {
            System.out.println("Filename: " + filePath.toString());
            System.out.println("Exist in location!");
        } else {
            System.out.println("Filename: " + filePath.toString());
            System.out.println("Does not exist in location!");
        }
    }

    /**
     * Method checkEmptyDirectory.
     * 
     * @param file
     *            File
     * @param filePath
     *            Path
     */
    private static void checkEmptyDirectory(File file, Path filePath) {
        if (file.isDirectory()) {
            if (file.list().length > 0) {
                checkFileExist(filePath);
            } else {
                System.out
                        .println("Directory specified does not contain any files!");
            }
        } else {
            System.out.println("Directory specified does not exist!");
        }
    }
}

1 Answers

Inorder to find any files in a folder try to do like this

File f= new File(filePathString);
File[] listOfFiles = f.listFiles(); 

if(listOfFiles.length > 0){
//
}else{
//
}
like image 104
karthick Avatar answered Sep 17 '25 00:09

karthick