PDF文件过滤

 1.需求:有大量的的PDF文件在同一文件夹下的不同文件夹里,看起来很不方便,想实现把这些文件全部过滤到指定的文件夹下查看。

 

 2.实现代码:

 Version 1:

  

package com.apidemo;

import java.io.File;
import java.io.FileFilter;

public class PdfFilter implements FileFilter {

	@Override
	public boolean accept(File pathname) {
		if(pathname.isDirectory())  
            return true; 
        else{
            String name = pathname.getName();  
            if(name.endsWith(".pdf"))  
                return true;  
            else  
                return false;  
        }  
	}
		
 public static void getAllFilePath(String rootPath){  
   
        File file = new File(rootPath);  
        File[] files = file.listFiles(new PdfFilter());  
         //最终路径
        // File result = new File("C:"+File.separator+"YourPDF");  
        for(int i=0;i<files.length;i++)  
        {  
        	if(files[i].isDirectory())  
            { 
               getAllFilePath(files[i].getPath());  
            }else{
             System.out.println(files[i].getPath());  
                            
            }
        }             
    }  

   public static void main(String[] args) {
	   //源文件路径
	   String path =  "E:"+File.separator+"xx"+File.separator+"xx"+File.separator+"xx"+File.separator+"xx";

	   getAllFilePath(path);
	
	}
 
}




3.改进空间:该版本未实现将PDF文件过滤至指定的文件夹里,只是过滤出来将其路径打印出来。


  Version 2:

  

package com.apidemo;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PdfFilter implements FileFilter {

	@Override
	public boolean accept(File pathname) {
		if (pathname.isDirectory())
			return true;
		else {
			String name = pathname.getName();
			if (name.endsWith(".pdf"))
				return true;
			else
				return false;
		}
	}

	public static void getAllFilePath(String rootPath,String resultPath) throws IOException{  
   
        File file = new File(rootPath);  
        File resultFile = new File(resultPath);
        
        if(!resultFile.exists()){
        	resultFile.mkdir();
        }
        
        FileInputStream  fis = new FileInputStream(resultFile);  
        FileOutputStream  fos = new FileOutputStream(file); 
        
        File[] files = file.listFiles(new PdfFilter());  
        
        byte[] buf = new byte[1024];
        
        for(int i=0;i<files.length;i++)  
        {  
        	if(files[i].isDirectory())  
            { 
               getAllFilePath(files[i].getPath(),resultPath);  
            }else{
            		
              fos.write(buf);
              fis.read(buf);
              System.out.println(files[i].getPath());  
            }
        } 
        
        if(fis!=null||fos!=null){
        	fis.close();
        	fos.close();
        }
    }
	
	public static void main(String[] args) throws IOException {
		// 源文件路径
		String path = "E:" + File.separator + "xx" + File.separator + "xx"
				+ File.separator + "xx" + File.separator + "xx";
		// 目标文件路径
	    String resultPath = "F:"+ File.separator+"PDFResult";
	    
		getAllFilePath(path,resultPath);

	}

}

目标:实现将PDF文件过滤至指定的文件夹里


问题:F:\PDFResult (拒绝访问。)

你可能感兴趣的:(java,pdf,文件过滤)