Reputation:
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!");
}
}
}
Upvotes: 4
Views: 11994
Reputation: 1165
i think this might will help you for check that in folder there is a file exist or not!
File folder = new File("/home/rahul/Documents/r/212/");
f.listFilesForFolder(folder);
}
public void listFilesForFolder(final File folder) {
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForFolder(fileEntry);
} else {
l.add(fileEntry.getName());
}
}
My code is getting name of all files available from directory if file exist in folder.
Upvotes: 0
Reputation: 8334
Using java.io.File
File f = new File(filePathString);
if(f.exists()) { /* do something */ }
In your code :
File f = new File(filePath);
if(f.exists()) {
if(f.isDirectory()){ System.out.println("it's a folder"); }
System.out.println("File location: " +filePath.toString());
System.out.println("File exist in location!");
System.exit(0);
}
Upvotes: 1
Reputation: 11
import java.io.*;
File file = new File(Path);
if(file.exists()){.........}
Upvotes: 1
Reputation: 39386
Here
"C:\\TEST" +todayDate +".txt"
You missed a folder separator. It should probably be:
"C:\\TEST\\" +todayDate +".txt"
Upvotes: 0
Reputation: 2153
This should do it http://www.mkyong.com/java/how-to-check-if-directory-is-empty-in-java/
public static void main(String[] args)
{
File file = new File("\myFolder");
if(file.isDirectory()){
if(file.list().length>0){
System.out.println("Directory is not empty!");
}else{
System.out.println("Directory is empty!");
}
}else{
System.out.println("This is not a directory");
}
}
Upvotes: 0
Reputation: 12176
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{
//
}
Upvotes: 7
Reputation: 9741
Count the listed files using File#list():
File f = new File(filePathString);
int nOF = f.list().length; //add null check before
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory. Names denoting the directory itself and the directory's parent directory are not included in the result. Each string is a file name rather than a complete path.
Upvotes: 0