我已经更新了我的代码来处理列出的场景 1. 检查空目录 2. 检查文件是否存在
当我尝试执行文件监视程序时,我还应该注意哪些其他事项?
我希望能够创建一些可以涵盖监视文件时可能发生的所有可能场景的东西,这样当我部署代码时,我可以快速读取输出并知道发生了什么
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!");
}
}
}