Skip to content

Instantly share code, notes, and snippets.

@NikolasTzimoulis
Created May 31, 2012 20:48
Show Gist options
  • Save NikolasTzimoulis/2846139 to your computer and use it in GitHub Desktop.
Save NikolasTzimoulis/2846139 to your computer and use it in GitHub Desktop.
A simple programme that finds all the file extensions of the files in a directory and recursively in its subdirectories
package main;
import java.io.File;
import java.util.LinkedList;
import java.util.Scanner;
public class GetExtensions
{
private static int fileCounter;
private static LinkedList<String> Extensions;
public static void main(String[] args)
{
fileCounter = 0;
Extensions = new LinkedList<String>();
Scanner read = new Scanner(System.in);
System.out.print("Directory: ");
String dirText = read.next();
AddToFileList(dirText);
java.util.Collections.sort(Extensions);
int extCounter = Extensions.size();
while(!Extensions.isEmpty())
{
System.out.println(Extensions.pop());
}
System.out.println("Total: " + extCounter + " extensions in " + fileCounter + " files.");
}
private static void AddToFileList(String dirText)
{
File dir = new File(dirText);
if (dir.isFile())
{
String[] pieces = dirText.split("[.]");
if(pieces.length>1)
{
String currentExtension = pieces[pieces.length-1].toLowerCase();
if(!Extensions.contains(currentExtension))
{
Extensions.add(currentExtension);
}
}
fileCounter++;
}
else
{
String[] ToAdd = dir.list();
for(int i=0;i<ToAdd.length;i++)
{
AddToFileList(dirText + "\\" + ToAdd[i]);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment