我在下面的程序中没有在指定的时间段内选择文件,请更新..其中有错误..
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PatternFileFilter implements FileFilter {
public static String ITEM_TYPE_FILE = "File"; // for file
public static String ITEM_TYPE_FOLDER = "Folder"; // for folder
public static String ITEM_TYPE_FILE_AND_FOLDER = "FileAndFolder"; // for file and folder
private Pattern fileNamePattern;
public PatternFileFilter(Pattern fileNamePattern) {
this.fileNamePattern = fileNamePattern;
}
public boolean accept(File pathname) {
return fileNamePattern.matcher(pathname.getName()).find() || pathname.isDirectory();
}
public Pattern getPattern() {
return fileNamePattern;
}
public static void searchFile(File topFolderOrFile, String type, PatternFileFilter filter, long timeOut) throws IOException {
long startTimeStamp = Calendar.getInstance().getTimeInMillis();
if (topFolderOrFile.isDirectory()) {
File[] subFoldersAndFileNames = topFolderOrFile.listFiles(filter);
if (subFoldersAndFileNames != null && subFoldersAndFileNames.length > 0) {
for (File subFolderOrFile : subFoldersAndFileNames) {
if (ITEM_TYPE_FILE.equals(type) && subFolderOrFile.isFile()) {
System.out.println("File name matched ----- " + subFolderOrFile.getName());
}
if (ITEM_TYPE_FOLDER.equals(type) && subFolderOrFile.isDirectory()
&& filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
System.out.println("Folder name matched ----- " + subFolderOrFile.getName());
}
if (ITEM_TYPE_FILE_AND_FOLDER.equals(type) && filter.getPattern().matcher(subFolderOrFile.getName()).find()) {
System.out.println("File or Folder name matched ----- " + subFolderOrFile.getName());
}
// You need to decide if you want to process the folders inline // or after you've processed the
// file list...
if (subFolderOrFile.isDirectory()) {
long timeElapsed = startTimeStamp - Calendar.getInstance().getTimeInMillis();
if (((timeOut * 1000) - timeElapsed) < 0) {
System.out.println("Could not complete operation-- timeout");
} else {
searchFile(subFolderOrFile, type, filter, (timeOut * 1000) - timeElapsed);
}
}
}
}
}
}
public static void searchFile(String topFolderName, String type, String fileNamePatternRegExp, long timeOut)
throws IOException {
File topFolderOrFile = new File(topFolderName);
Pattern fileNamePattern = Pattern.compile(fileNamePatternRegExp);
searchFile(topFolderOrFile, type, new PatternFileFilter(fileNamePattern), timeOut);
}
// ***************
public static void main(String[] str) throws Exception {
System.out.println("Type Item to Search ");
System.out.println("1 File");
System.out.println("2 Folder ");
System.out.println("3 Both");
System.out.println("0 Exit");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String searchType = br.readLine();
System.out.println("Enter top folder name ::");
String topFolderName = br.readLine();
System.out.println("Enter name of file to search ::");
String fileName = br.readLine();
System.out.println("Enter timeout period in seconds::");
String timeOutStr = br.readLine();
if (searchType == null || fileName == null || topFolderName == null || timeOutStr == null) {
throw new Exception("Error Occured::Provide both the input Parameters");
}
int searchTypeInd = Integer.parseInt(searchType);
switch (searchTypeInd) {
case 1:
searchFile(topFolderName, ITEM_TYPE_FILE, fileName, Long.parseLong(timeOutStr));
break;
case 2:
searchFile(topFolderName, ITEM_TYPE_FOLDER, fileName, Long.parseLong(timeOutStr));
break;
case 3:
searchFile(topFolderName, ITEM_TYPE_FILE_AND_FOLDER, fileName, Long.parseLong(timeOutStr));
break;
case 0:
System.exit(0);
}
}
}
请告知我D:\ saral中是否有文件夹,应该从D:文件夹中选择文件
我得到的结果是......
键入要搜索的项目1文件2文件夹3两者0退出3输入顶级文件夹 name :: Test输入要搜索的文件名:: allMfile.txt输入 超时时间(秒):: 5
答案 0 :(得分:1)
我猜这是错的:
searchFile(subFolderOrFile, type, filter, (timeOut * 1000) - timeElapsed);
^^^^^^^
看起来你混合了时间单位:你期望秒,但是传递毫秒值。在你的情况下,你传递的第一个超时等于5秒,在第一次递归中,你传递5000秒,在下一级传递1000次......
提示:将方法参数重命名为timeOutInSeconds
并再次读取您的代码。
答案 1 :(得分:1)
我建议颠倒逻辑。而不是传递超时(并进行复杂的计算),计算方法之外的结束时间:
long timeOut = System.currentTimeMillis() + Long.parseLong(timeOutStr);
在该方法中,您可以使用以下方法检查超时:
if( System.currentTimeMillis() > timeOut ) { break; }
您也不会在代码中结束循环(使用break
或return
),因此检查超时只会阻止递归调用。
最后,请考虑反转您的if()
条件而不是嵌套它们:
if (!topFolderOrFile.isDirectory()) { return; }
File[] subFoldersAndFileNames = topFolderOrFile.listFiles(filter);
if (subFoldersAndFileNames == null || subFoldersAndFileNames.length == 0) { return; }
...