skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Friday, September 21, 2012

Reading filenames with specific extensions from a folder and writing into excel

Posted by Admin at 1:13 PM – 0 comments
 
This utlity program reads the names of all files in a folder and generate a report enlisting filenames based on selected file extensions only via java program. This standalone program extracts names of files with .doc extension. The program can be customized to enable export of filenames into an excel. all file names or files with .xls or .pdf extension can be enlisted in an excel.
1. Add the JXl.jar file to the classpath of the project.
2. Add the java file in a project(change pachakge name in applicable)
3. Change the source folder location and if required, name of the excel file to be generated.
4. Run the GetFolderContent.java file

Excel file will be generated in the corresponding project folder.


?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
 
public class GetFolderContent {
 
 private static String excelFileName = "FileList.xls";
 private static String targetexcelFilepath = "C:\backup\test\Test_Work";  // generated excel file location
 private static String sourceFolderPath = "C:\DUK-CC"; // source Directory
 private static int totalFileCount = 0; // variable to store total file count
 private static int fileCount = 0; // variable to store required files count
 private static ArrayList<String> aList = new ArrayList<String>();
 
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  // to get file names
  File folder = new File(sourceFolderPath);
  File[] listOfFilesInDirectory = folder.listFiles();
  try {
   for (int numberOfFilesInFolder = 0; numberOfFilesInFolder < listOfFilesInDirectory.length; numberOfFilesInFolder++) {
 
    if (listOfFilesInDirectory[numberOfFilesInFolder].isFile()) {
     totalFileCount++;
     String files = listOfFilesInDirectory[numberOfFilesInFolder]
       .getName();
     if (files.endsWith(".doc") || files.endsWith(".DOC")
       || files.endsWith(".DOCX")
       || files.endsWith(".docx")) {
      aList.add(files);
      fileCount++;
     }
    }
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("Total file count = " + totalFileCount);
  System.out.println("Required file count = " + fileCount);
 
  createExcelFileAndGetContent();
 }
 
 // code to generate an excel file
 public static void createExcelFileAndGetContent() {
 
  File file = new File(excelFileName);
 
  try {
   WorkbookSettings wbSettings = new WorkbookSettings();
   WritableWorkbook workbook = Workbook.createWorkbook(file,
     wbSettings);
   workbook.createSheet("File Names", 0);
   System.out.println("excel File is created in path -- "
     + targetexcelFilepath+ "n*********Items in directory:**********" );
   WritableSheet excelSheet = (WritableSheet) workbook.getSheet(0);
   if (!aList.isEmpty()) {
    for (int rowNumber = 0; rowNumber < aList.size(); rowNumber++) {
     System.out.println(aList.get(rowNumber));
     Label label = new Label(1, rowNumber, (String) aList.get(rowNumber));
     excelSheet.addCell(label);
    }
   } else {
    System.out.println("No matching files found");
   }
   workbook.write();
   workbook.close();
  } catch (RowsExceededException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IndexOutOfBoundsException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (WriteException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
 }
 
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger