0

I build java web Application.

I wrote 1 function in my class with 2 argument.If you pass directory path(where .txt files are saved) and filetype as a arguments to that function.It returns all the filenames,which files have with specified file extension.

  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;
}

I tested this function in the following 2 ways.

Case 1:

I created folder name as InputFiles on my desktop and placed .txt files under InputFiles folder.

I pass directoryPath and .txt as a arguments to my function in the following way.It's working fine.

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

Case 2:

Now I placed my InputFiles folder under src folder and pass directoryPath as a argument in the following way.it's not working.

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

Why I am trying case 2,If I want to work on same Application in another system,everytime I don't need to change directorypath.

At deployment time also case 2 is very useful because,we don't know where will we deploy Application.so I tried case 2 it's not working.

It's working,when I mention absolute path.If I mention realPath it's not working.

How can I fix this.

can you explain clearly.

I hope, you understand why I am trying case 2.

Thanks.

ur truly friend
  • 553
  • 4
  • 11
  • 23

1 Answers1

0

well you can always user property class. Just set the path in property file and get that property by name in your class. Also if you ever feel like that you need to change the path mentioned in property file, it will get reloaded as soon as you make change in it.

streak
  • 1,121
  • 1
  • 19
  • 28