2013-07-29 24 views
0

我構建java web應用程序。我如何提到Java web應用程序中的目錄路徑

我在我的課寫了1個函數2 argument.If傳遞目錄路徑(其中.txt文件保存)和文件類型作爲參數到function.It返回所有的文件名,哪些文件具有與指定的文件擴展名。

public List<File> ListOfFileNames(String directoryPath,String fileType) 
{ 
    //Creating Object for File class 
    File fileObject=new File(directoryPath); 
    //Fetching all the FileNames under given Path 
    File[] listOfFiles=fileObject.listFiles(); 
    //Creating another Array for saving fileNames, which are satisfying as far our requirements 
    List<File> fileNames = new ArrayList<File>(); 
    for (int fileIndex = 0; fileIndex < listOfFiles.length; fileIndex++) 
    { 
     if (listOfFiles[fileIndex].isFile()) 
     { 
      //True condition,Array Index value is File 
      if (listOfFiles[fileIndex].getName().endsWith(fileType)) 
      { 
       //System.out.println(listOfFiles[fileIndex].getName()); 
       fileNames .add(listOfFiles[fileIndex]); 
      } 
     } 
    } 
    return fileNames; 
} 

我用以下兩種方式測試了這個函數。

案例1:

我在我的桌面上創建的文件夾名稱爲InputFiles,並放置InputFiles文件夾下的文件.txt

我通過directoryPath.txt作爲我的功能的參數在以下方式。它工作正常。

classNameObject.Integration("C:/Documents and Settings/mahesh/Desktop/InputFiles",".txt"); 

案例2:

現在我把我的文件夾InputFilessrc下,並通過directoryPath在way.it不工作下一個參數。

classNameObject.Integration("/InputFiles",".txt"); 

爲什麼我想情況2,如果我想在另一個系統相同的應用工作,每次我不需要改變directorypath

在部署時也case 2是非常有用的,因爲我們不知道我們將在哪裏部署Application.so我試過case 2它不工作。

它的工作,當我提到絕對路徑。如果我提到realPath它不起作用。

我該如何解決這個問題。

你能解釋清楚嗎?

我希望你明白我爲什麼要嘗試案例2.

謝謝。

+0

[看看這個](http://stackoverflow.com/q/2308188/1679863) –

+0

@我修改了我的問題,你可以檢查一次 –

回答

0

以及你可以隨時用戶的財產類。只需在屬性文件中設置路徑並在您的類中按名稱獲取該屬性即可。此外,如果您覺得您需要更改屬性文件中提及的路徑,只要您在其中進行更改就會重新加載。

+0

我剛接觸'Java'技術,我會從哪裏得到'屬性文件'。你能一步一步解釋嗎? –

+0

chk此鏈接: - http://www.mkyong.com/java/java-properties-file-examples/ – streak

+0

我修改了我的問題,可以檢查一次。 –

相關問題