使用java从多个文件中搜索pdf文件
我们只想从一个文件夹中的多个pdf文件中提取特定的pdf文件,使用java代码。它必须是web视图。请帮助我使用java从多个文件中搜索pdf文件,java,Java,我们只想从一个文件夹中的多个pdf文件中提取特定的pdf文件,使用java代码。它必须是web视图。请帮助我 package manju1; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser.PdfTextExtractor; import java.awt.Desktop; import java.io.File; import java.io.IOException; import jav
package manju1;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Manju1
{
public static void main (String args[]) throws IOException
{
File location= new File("c:\\testpdf\\");
String type = ".pdf"; // replace what ever type of file you need to search
if (location.isDirectory() && location != null)
{
for (File f : location.listFiles())
{
if (f.isFile() && f.getName().endsWith(".pdf"))
{
//System.out.println(f.getName());
System.out.println("enter the first string");
Scanner in=new Scanner(System.in);
String m=in.nextLine();
if(m.equals(f.getName()))
{
System.out.println("Strings are equal");
PdfReader reader = new PdfReader("c:\\testpdf\\swathi.pdf");
System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
String page = PdfTextExtractor.getTextFromPage(reader,1);
System.out.println("Page Content:\n\n"+page+"\n\n");
break;
}
else
{
System.out.println("Strings are not equal");
}
}
}
}
}
该程序默认情况下按顺序存储文件,并检查输入的字符串是否匹配,然后显示该字符串,否则不显示该字符串。我们需要一个代码来获取pdf文件时,我们从键盘输入不正常。如果我们搜索的文件存在于该文件夹中,它将显示它。那么您想在特定目录中查找一个文件,该文件的名称由用户提供 您可以执行以下操作,而不是搜索所有文件:
File location= new File("c:\\testpdf\\");
String type = "pdf";
if (location != null && location.isDirectory()){
Scanner sc = new Scanner(System.in);
String fileName = sc.nextLine();
File file = new File(location, fileName);
if(file.exists() && FilenameUtils.getExtension(file.getAbsolutePath()).equals(type)){
//The file exists
}
else{
//the file doesnt exist
}
}
}
我想指出的是,我知道
if(location!=null&&location.isDirectory())
而不是相反,这样,如果location为null,就不会得到NullPointerException
如果我误解了,或者不清楚,请告诉我对不起,我无法理解。上面的代码做了什么您不希望它做的事情?请同时改进代码格式和标题。您好,非常感谢您的帮助。但是我在下面提到的这行中遇到了一个错误。如果(file.exists()&&FilenameUtils.getExtension(file.getAbsolutePath()).equals(type))。尤其是在FilenameUtils.getExtension中。请帮我解决这个问题。但是我需要你的要求你已经很清楚了。谢谢你。我希望你能尽快回复我。