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 .
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());
}
}
}
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); } } } }
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);} }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With