Get all file names in given directory – Java IO

Java IO – Code to get all file names in a given directory

Java IO code to get the list of file names in a given directory. This will iterate through all the directories and get the files names from each of the folder.

Input: Pass the path / directory

Returns: File names in a array format.

 

Java IO

Java IO

Code to get all file names in a given directory

public static String[] getAllFileNames(String pFilePath) throws IOException
{
   List<String> fileNames = new ArrayList<String>();
   File fileDir = new File(pFilePath);
   if (fileDir != null)
   {
      // get list of all files
      File[] aFiles = fileDir.listFiles();
      if (aFiles != null)
      {
          for (int x = 0; x < aFiles.length; x++) {
               // ignore directories
               if (aFiles[x].isDirectory()) {
                     continue;
                }
                fileNames.add(aFiles[x].getCanonicalPath());
            } // end for()
        }
    }
    String[] fileNamesArray= new String[0];
    return (String[]) fileNames.toArray(fileNamesArray);
}

——————————————————————————–

You may also like this

——————————————————————————–

About TechnoStixs.com

TechnoStixs

TechnoStixs.com is a dedicated weblog for Java/J2EE and web developers. We take pride in our work. Every publication is carefully analyzed, written and tested to ease the understanding of the topic or subject.

We cover Java Core Technologies, eCommerce, J2EE Frameworks, Web Service, Build Tools, Unit Test Frameworks and Others.

961 total views, 1 views today

Add comment