尝试开发一个程序,该程序将搜索给定路径以查找文件(如果用户提供名称或只搜索所有文件),然后查找ext,内容和修改的最后日期。我无法让内容部分正常工作(所以当我进入狗时,我就是其中包含内容的所有文件)&我还没有开始修改最后修改日期。我一直在尝试使用contains.name,但它无法正常工作。谢谢你的帮助!
现在我的代码现在出现了这个错误。
Main.java:31: error: unreported exception IOException; must be caught or declared to be thrown
return name.toLowerCase().startsWith(fileN.toLowerCase()) && name.toLowerCase().endsWith("." + ext.toLowerCase()) && find(name, content);
^
1 error
当我没有抛出我的find方法时,我得到了这个输出:
Search by path, name, extension, content and date.
Enter starting directory for the search (like c:\temp): /Users/KaylaSiemon/Documents
Enter the file name (like myFile or enter for all):
Enter the file extension (like txt or enter for all): docx
Enter content to search for (like cscd211 or enter for any): kayla
Enter last modified date (like 11/21/2013 or enter for any): kj
java.io.FileNotFoundException: COVER LETTER.docx (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at Main.find(Main.java:53)
at Main$1.accept(Main.java:31)
at java.io.File.list(File.java:1155)
at Main.main(Main.java:34)
程序代码:
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws FileNotFoundException
{
Scanner kb = new Scanner(System.in);
System.out.println("Search by path, name, extension, content and date.\n\n");
System.out.print("Enter starting directory for the search (like c:" + "\\" + "temp): ");
String direct = kb.nextLine();
File dir = new File(direct);
System.out.print("Enter the file name (like myFile or enter for all): ");
final String fileN = kb.nextLine();
System.out.print("Enter the file extension (like txt or enter for all): ");
final String ext = kb.next();
System.out.print("Enter content to search for (like cscd211 or enter for any): ");
final String content = kb.next();
System.out.print("Enter last modified date (like 11/21/2013 or enter for any): ");
String date = kb.next();
System.out.println();
FilenameFilter filter =
new FilenameFilter() {
public boolean accept
(File dir, String name) {
Scanner input = new Scanner(name);
return name.toLowerCase().startsWith(fileN.toLowerCase()) && name.toLowerCase().endsWith("." + ext.toLowerCase()) && find(name, content);
}
};
String[] children = dir.list(filter);
if (children == null) {
System.out.println("Either dir does not exist or is not a directory");
}
else {
for (int i=0; i < children.length; i++) {
String filename = children[i];
System.out.println(dir + "/" +filename);
}
}
}
public static boolean find(String f, String searchString) throws IOException
{
boolean result = false;
Scanner in = null;
try
{
in = new Scanner(new FileReader(f));
while(in.hasNextLine() && !result) {
result = in.nextLine().indexOf(searchString) >= 0;
}
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch(Exception e) {
/*ignore*/
}
}
return result;
}
}
答案 0 :(得分:0)
您需要将整个文件IO代码放在以下块中:
try{
//your code here
}catch(IOException e){
//code to handle exception if there is a problem with the file
}
答案 1 :(得分:0)
Main.java:31: error: unreported exception IOException; must be caught or declared to be thrown
return name.toLowerCase().startsWith(fileN.toLowerCase()) && name.toLowerCase().endsWith("." + ext.toLowerCase()) && find(name, content);
^
1 error
告诉你正在调用的方法是抛出IOException
,但异常未经处理。它既没有被捕获也没有被重新抛出。
此时您可以将语句包装在try-catch
子句中,因为您将无法重新抛出该语句,但更多的调查可能会提供更好的替代方案......
find
方法实际上并不需要抛出IOException
因为你已经处理过它...但是,我认为这是一个坏主意,因为调用者应该处理例外......赶上22 ......
相反,您的FilenameFilter
看起来更像......
FilenameFilter filter
= new FilenameFilter() {
public boolean accept(File dir, String name) {
Scanner input = new Scanner(name);
boolean accept = false;
try {
accept = name.toLowerCase().startsWith(fileN.toLowerCase()) && name.toLowerCase().endsWith("." + ext.toLowerCase()) && find(name, content);
} catch (IOException e) {
e.printStackTrace();
}
return accept;
}
};
您的find
方法更像......
public static boolean find(String f, String searchString) throws IOException {
boolean result = false;
Scanner in = null;
try {
in = new Scanner(new FileReader(f));
while (in.hasNextLine() && !result) {
result = in.nextLine().indexOf(searchString) >= 0;
}
} finally {
try {
in.close();
} catch (Exception e) {
/*ignore*/
}
}
return result;
}
现在...
java.io.FileNotFoundException: COVER LETTER.docx (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at Main.find(Main.java:53)
at Main$1.accept(Main.java:31)
at java.io.File.list(File.java:1155)
at Main.main(Main.java:34)
是一些事情的结果......
首先,您的FilenameFilter
传递了两件事,dir
作为File
,name
作为String
。 name
仅包含文件的名称,它不包含文件所在的目录,这是dir
参数的目的......
其次,您指定了一些任意路径,在本例中为/Users/KaylaSiemon/Documents
第三,程序运行的位置可能与您尝试搜索的路径不一样......
所有这些意味着,当您尝试执行in = new Scanner(new FileReader(f));
时,Java实际上是查找文件的当前目录,而不是您之前指定的目录。
也就是说,它不是在寻找/Users/KaylaSiemon/Documents/COVER LETTER.docx
,而是在寻找./COVER LETTER.docx
最简单的解决方法是更改find
方法的参数以使用File
引用而不是String
,File
提供更好的路径上下文解析String
可能......
例如......
public static boolean find(File f, String searchString) throws IOException {
这意味着当您调用它时,您需要将File
引用传递给它,例如
find(new File(dir, name), content)