2016-03-01 17 views
0

運行鍼對很多參數的程序作爲文件這是程序的基本佈局,我有:如何從一個文件夾

public class myClass { 

    public void run(String[] args) throws Exception { 
     // do something with the file 
    } 

    public static void main(String[] args) throws Exception { 
     new myClass().run(args); 
    } 
} 

我在Eclipse中設置的參數是我的驅動器上的文件位置,而我可以複製運行配置並添加我想運行的每個文件,如果我有數百個文件,那麼這不是一個非常有效的方法,如果文件經常發生更改,那麼這種方法就更少了。理想情況下,我希望能夠指定一個文件夾,並讓我的程序針對具有特定擴展名的文件夾中的每個文件運行,例如「.txt」。這可能在日食或我必須寫腳本來實現這個或其他東西?如果需要一個腳本,一些技巧將非常值得讚賞,因爲我不熟悉腳本編寫。

+0

你不告訴你的程序的目的是什麼。從那開始...... – fge

+0

是的,您可以在Java代碼中的特定目錄中檢索文件名。請參閱https://docs.oracle.com/javase/tutorial/essential/io/dirs.html#listdir –

+0

創建一個[FileVisitor](https://docs.oracle.com/javase/7/docs/api/java /nio/file/FileVisitor.html)並瀏覽目錄中的文件,然後檢查擴展名。查看[Java IO API](https://docs.oracle.com/javase/7/docs/api/java/io/package-summary.html) – callyalater

回答

0

你可以做一些事情,像這樣:

public class myClass implements FileFilter { 

     public void run(File[] files) throws Exception { 
      // do something with the file 
     } 

     public static void main(String[] args) throws Exception { 
      File folder = new File(folderPathString); 
      new myClass().run(folder.listFiles(new myClass())); 
     } 

     public boolean accept(File pathname) { 
      return pathname.getName().endsWith(".txt"); 
     } 
    } 
相關問題