gpt4 book ai didi

java - 检查给定文件夹中是否存在任何文件

转载 作者:行者123 更新时间:2023-11-29 07:54:23 25 4
gpt4 key购买 nike

我已经更新我的代码来处理列出的场景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!");
}
}
}

最佳答案

为了找到文件夹中的任何文件,请尝试这样做

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

if(listOfFiles.length > 0){
//
}else{
//
}

关于java - 检查给定文件夹中是否存在任何文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com