1public static void main(String[] args) {
2 String dirPath = "D:\\temp\\";
3 File dir = new File(dirPath);
4
5 // Get Directory Listing
6 File[] fileList = dir.listFiles();
7
8 // Process each file
9 for(int i=0; i < fileList.length; i++)
10 {
11 if(fileList[i].isFile()) {
12 String fileName = fileList[i].getName();
13 // Split at the file extension and the classgroup
14 String[] fileParts = fileName.split("[_\\.]");
15 System.out.println("One: " + fileParts[0] + ", Two: " + fileParts[1]);
16
17 // Check directory exists
18 File newDir = new File(dirPath + fileParts[0] + "\\" + fileParts[1]);
19 if(!newDir.exists()) {
20 // Create directory
21 if(newDir.mkdirs()) {
22 System.out.println("Directory Created");
23 }
24 }
25
26 // Move file into directory
27 if(fileList[i].renameTo(new File(dirPath + fileParts[0] + "\\" + fileParts[1] + "\\" + fileName))) {
28 System.out.println("File Moved");
29 }
30
31 }
32 }
33}
34