文件查找(备注)

package cn.demo.action;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStreamReader;


public class Test implements FileFilter{
	//先判断操作系统,true为window系统否则为liunx系统
	public static boolean isWindowsOS(){
	    boolean isWindowsOS = false;
	    String osName = System.getProperty("os.name");
	    if(osName.toLowerCase().indexOf("windows")>-1){
	      isWindowsOS = true;
	    }
	    return isWindowsOS;
	 }
	
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
	    File[] roots=File.listRoots();
	    for(int i=0;i< roots.length;i++){
	      System.out.println(roots[i].getPath());
	    }
	    
	    System.out.println("请输入要查找的盘:"); 
	    BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in)); 
	    String filename1 = in1.readLine(); 
	    File file1 = new File(filename1); 
	    File[] fox = file1.listFiles(); 
	    String[] filelist1 = file1.list(); 
	    int length1; 
	    try { 
	      length1 = filelist1.length; 
	    } 
	    catch (NullPointerException e) { 
	      throw new NullPointerException("错误:没有输入正确的要查找的文件所在盘"); 
	    } 

	    //输入文件或文件夹 
	    System.out.println("请输入要查找的文件或文件夹:"); 
	    BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in)); 
	    String filename2 = in2.readLine(); 
	    File file2 = new File(filename1); 
	    String[] filelist2 = file2.list(); 

	   

	    //查找文件或文件夹 
	    int f = 0; 
	    for (int i = 0; i < length1; i++) { 
	      if (filelist1[i].indexOf(filename2) >= 0) { 
	        System.out.println(fox[i]); 
	        f++; 
	      } 
	    } 
	    if (f <= 0) { 
	      System.out.println("没有你要找的文件"); 
	    } 
	  	    
	}

	public boolean accept(File pathname) {
		
		return false;
	}

}

你可能感兴趣的:(java,windows,OS,F#)