2014-02-06 40 views
0

我正在創建一個簡單的文件開啓器/文件搜索器,我用它編寫了一個java代碼。我也進行了三重檢查,以確保我在正確的地方有正確的文件。如何在特定目錄中搜​​索並存檔,然後打開該文件?

import java.io.BufferedReader; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

public class FileSearch { 

    private String fileNameToSearch; 
    private List<String> result = new ArrayList<String>(); 

    public String getFileNameToSearch() { 
    return fileNameToSearch; 
    } 

    public void setFileNameToSearch(String fileNameToSearch) { 
    this.fileNameToSearch = fileNameToSearch; 
    } 

    public List<String> getResult() { 
    return result; 
    } 

    public static void main(String[] args) throws IOException { 

     FileSearch fileSearch = new FileSearch(); 

     //try different directory and filename :) 
     fileSearch.searchDirectory(new File("/test/this"), "notepad.exe"); 

     int count = fileSearch.getResult().size(); 
     if(count ==0){ 
      System.out.println("\nNo result found!"); 
     } else { 
      System.out.println("\nFound " + count + " result!\n"); 
      for (String matched : fileSearch.getResult()){ 
       System.out.println(matched); 
       String newMatched = matched.replace("\\", "\\\\"); 
       System.out.print(newMatched); 
       FileSearch.Openfiles(newMatched); 
      } 
     } 
    } 

    public void searchDirectory(File directory, String fileNameToSearch) { 

     setFileNameToSearch(fileNameToSearch); 

     if (directory.isDirectory()) { 
      search(directory); 
     } else { 
      System.out.println(directory.getAbsoluteFile() + " is not a directory!"); 
     } 

    } 

    private void search(File file) { 

     if (file.isDirectory()) { 

      //do you have permission to read this directory?  
      if (file.canRead()) { 
       for (File temp : file.listFiles()) { 
        if (temp.isDirectory()) { 
         search(temp); 
        } else { 
         if (getFileNameToSearch().equals(temp.getName().toLowerCase())) {   
          result.add(temp.getAbsoluteFile().toString()); 
         } 
        } 
       } 

      } else { 
       System.out.println(file.getAbsoluteFile() + "Permission Denied"); 
      } 
     } 
    } 

    public static void Openfiles(String open) throws IOException { 
     List<String> command = new ArrayList<String>(); 
     command.add(open); 

     ProcessBuilder builder = new ProcessBuilder(command); 
     Map<String, String> environ = builder.environment(); 

     final Process process = builder.start(); 
     java.io.InputStream is = process.getInputStream(); 
     InputStreamReader isr = new InputStreamReader(is); 
     BufferedReader br = new BufferedReader(isr); 
     String line; 
    } 

} 

這是我問的這個網站的網站,所以我很抱歉,如果我的代碼格式不正確,我的第一個問題。我把這段代碼放進去,它完全運行完全,沒有任何錯誤,但它不運行notepad.exe。我嘗試了一個文本文件,它給了我「createProcess error = 193,%1不是有效的win 32應用程序」我在coderanch上閱讀,這意味着它不是一個可執行文件,所以我改變了它。我非常努力地搜索谷歌,雅虎和Bing。我甚至嘗試過一些metasearchers。我已經問過每個人,我知道他知道Java,他們也無法弄清楚。對不起,我寫了任何不好的英語。請儘快幫助我。先謝謝你。

+0

您也可以使用本機JfileChoser:http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html – ltalhouarne

回答

0

您可以使用此方法將文件從創立與JFileChooser所指定的目錄中複製

public void Copy(){ 
       // Create one directory 
      boolean dirFlag = false; 

      // create File object 
      File stockDir = new File(System.getProperty("user.dir")+"\\StockPhotosEtudiants/"); 
    // change this to you directory as you wich 

      try { 
       if(stockDir.exists()) 
         {System.out.println("Déja Creer");} 
       else {`// Creating directory` 
dirFlag = stockDir.mkdir(); 
         } 
      } catch (SecurityException Se) { 
      JOptionPane.showMessageDialog(this,"Error while creating directory in Java:'Nouveau Etud" + Se,"warning",JOptionPane.WARNING_MESSAGE); 

      } 


    inputFile = new File(fichier.getDirectory()+fichier.getFile()); 
    //extract the name of the file that you wish copying "D:\\" 
    outputFile = new File(System.getProperty("user.dir")+"\\StockPhotosEtudiants/"+jTextField4.getText()+".png"); 
    while(outputFile.exists()){ 
    outputFile = new File(System.getProperty("user.dir")+"\\StockPhotosEtudiants/"+jTextField4.getText()+"("+i+").png"); 
    i++; 
    } 
    UrlPhoto = System.getProperty("user.dir")+"\\StockPhotosEtudiants\\"+outputFile.getName(); 


     try { 

      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inputFile), 4096); 
      BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFile), 4096); 
       int theChar; 
       while ((theChar = bis.read()) != -1) { 
        bos.write(theChar); 
       } 
      bos.close(); 
      bis.close(); 

     } 
     catch(Exception e){e.printStackTrace();} 


     } 
0

我對你的搜索方法做了一些修改,和它的工作:

fileSearch.searchDirectory(new File("C:/Windows"), "notepad.exe"); 

搜索方法

private void search(File file) { 
    if (file != null && file.isDirectory()) { 
     File[] files = file.listFiles(); 
     if (files != null) { 
      for (File temp : files) { 
       if (temp != null) search(temp); 
      } 
     } 
    } else if (getFileNameToSearch().equals(file.getName())) { 
     result.add(file.getAbsoluteFile().toString()); 
    } 
} 

The無效檢查可能看起來過度,但在我的嘗試中它們是必需的。而且,在與C:\ Windows一樣大的目錄中,可能需要一段時間。

將notepad.exe從其目錄複製到測試目錄,我能夠找到匹配的搜索,但無法啓動可執行文件。這個原因超出了我的理解。

+0

謝謝您回答我的問題。 – user3281749

+0

將問題標記爲答案是感謝的方式,祝你好運 – pedromss

相關問題