指定された期間内にファイルを選択していない以下のプログラムがあります。更新してください..何が問題なのか..
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: フォルダーからファイルを選択する必要があります。
私が得ている結果は...
Type Item to Search 1 File 2 Folder 3 Both 0 Exit 3 トップフォルダ名を入力 :: Test 検索するファイル名を入力 :: allMfile.txt タイムアウト時間を秒単位で入力:: 5