Created
May 31, 2012 20:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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