Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java code to search all .doc and .docx files from local system

i am working in a desktop application for windows version using java. In my application there is a requirement to search all .doc and .docx files from the MyDocuments/Documents (as per O.S.) from local system and display there name and file size.

I am not getting the way that will help me to list out all the *.doc, *.docx, *.xls, *.xlsx, *.csv, *.txt, *.pdf, *.ppt, *.pptx files present in Documents/MyDocuments.

Please give me your valuable suggestions or suggest me any link that will help me in writing code for making a faster search and listing out with it's Name,size and Type .

like image 206
Toman Avatar asked Nov 10 '10 11:11

Toman


3 Answers

You can use Apache Commons IO, in particular the FileUtils class. That would give something like:

import java.io.File;
import java.util.Collection;

import org.apache.commons.io.*;
import org.apache.commons.io.filefilter.*;

public class SearchDocFiles {
    public static String[] EXTENSIONS = { "doc", "docx" };

    public Collection<File> searchFilesWithExtensions(final File directory, final String[] extensions) {
        return FileUtils.listFiles(directory,
                extensions,
                true);
    }

    public Collection<File> searchFilesWithCaseInsensitiveExtensions(final File directory, final String[] extensions) {
        IOFileFilter fileFilter = new SuffixFileFilter(extensions, IOCase.INSENSITIVE);
        return FileUtils.listFiles(directory,
                fileFilter,
                DirectoryFileFilter.INSTANCE);
    }


    public static void main(String... args) {
        // Case sensitive
        Collection<File> documents = new SearchDocFiles().searchFilesWithExtensions(
                new File("/tmp"),
                SearchDocFiles.EXTENSIONS);
        for (File document: documents) {
            System.out.println(document.getName() + " - " + document.length());
        }

        // Case insensitive
        Collection<File> caseInsensitiveDocs = new SearchDocFiles().searchFilesWithCaseInsensitiveExtensions(
                new File("/tmp"),
                SearchDocFiles.EXTENSIONS);
        for (File document: caseInsensitiveDocs) {
            System.out.println(document.getName() + " - " + document.length());
        }
    }
}
like image 176
Sébastien Le Callonnec Avatar answered Nov 14 '22 21:11

Sébastien Le Callonnec


Check this method.

public void getFiles(String path) {
    File dir = new File(path);
    String[] children = dir.list();
    if (children != null) {
        for (int i = 0; i < children.length; i++) {
            // Get filename of file or directory
            String filename = children[i];
            File file = new File(path + File.separator + filename);
            if (!file.isDirectory()) {
                if (file.getName().endsWith(".doc") || file.getName().endsWith(".docx")) {
                    System.out.println("File Name " + filename + "(" + file.length()+"  bytes)");
                }
            } else {
                getFiles(path + File.separator + filename);
            }
        }
    }
}
like image 26
Joe Avatar answered Nov 14 '22 23:11

Joe


If you want to find all the files with .doc(x) extensions, you can use java.io.File.list(FileFilter) method, say:

public java.util.List mswordFiles(java.io.File dir) {
   java.util.List res = new java.util.ArrayList();
   _mswordFiles(dir, res);
   return res;
}
protected void _mswordFiles(java.io.File dir, java.util.List res) {
   java.io.File [] files = dir.listFiles(new java.io.FileFilter() {
        public boolean accept(java.io.File f) {
           String name = f.getName().toLowerCase();
           return !f.isDirectory() && (name.endsWith(".doc") || name.endsWith(".docx"));
        }
     });
   for(java.io.File f:files) {res.add(f);}
   java.io.File [] dirs = dir.listFiles(new java.io.FileFilter() {
        public boolean accept(java.io.File f) {
            return f.isDirectory();
        }
      });
   for(java.io.File d:dirs) {_mswordFiles(d, res);}
}
like image 36
khachik Avatar answered Nov 14 '22 22:11

khachik