我们想要使用java代码从文件夹中的多个pdf文件中获取特定的pdf文件,它必须是web view.please请帮助我......
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文件。如果我们搜索的文件存在于该文件夹中,则应显示该文件。
答案 0 :(得分:0)
所以你想在特定目录中找到一个文件,其名称由用户提供?
您可以执行以下操作,而不是搜索所有文件:
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
如果我误解了,或者不清楚,请告诉我