Lookup Classpath Files : Classpath « Class « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Class » ClasspathScreenshots 
Lookup Classpath Files
     
//package com.myapp.util.file;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class FileUtils {
    
    public Collection<File> lookupClasspathFiles(String name) {
        Enumeration<URL> resources;
        List<File> files = new ArrayList<File>();
        
        try {
            resources = FileUtils.class.getClassLoader().getResources(name);
            
            while (resources.hasMoreElements()) {
                URI uri = resources.nextElement().toURI();
                File file = new File(uri);
                files.add(file);
            }
            
        catch (Exception e) {
            throw new RuntimeException("could not lookup files with name: "+name, e);
        }
        
        return files;
    }
    
    public static class FileFileFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            return pathname.isFile();
        }
    }


    public static class DirectoryFileFilter implements FileFilter {
        @Override
        public boolean accept(File pathname) {
            return pathname.isDirectory();
        }
    }


    public static final class RecursiveFileIterator implements Iterator<File> {
        private final List<File> recursiveListing;
        private final int size;
        private int nextIndex = -1;

        public RecursiveFileIterator(File root,
                                     boolean includeDirs,
                                     boolean includeFiles) {
            List<File> l = new ArrayList<File>();
            traverse(root, includeDirs, includeFiles, l);
            recursiveListing = Collections.unmodifiableList(l);
            size = recursiveListing.size();

            if (size > 0) {
                nextIndex = 0;
            }
        }

        private List<File> traverse(File startDir,
                                    final boolean includeDirs,
                                    final boolean includeFiles,
                                    List<File> toAdd) {
            validateDirectory(startDir);
            File[] filesAndDirs = startDir.listFiles();
            Arrays.sort(filesAndDirs);
            File tmp = null;
            boolean isDir, isFile;

            for (int i = 0; i < filesAndDirs.length; i++) {
                tmp = filesAndDirs[i];
                isDir = tmp.isDirectory();
                isFile = tmp.isFile();

                if (isDir) {
                    if (includeDirs)
                        toAdd.add(tmp);

                    traverse(tmp, includeDirs, includeFiles, toAdd);

                else if (isFile) {
                    if (includeFiles)
                        toAdd.add(tmp);
                }
            }

            return toAdd;
        }

        @Override
        public File next() {
            File next = recursiveListing.get(nextIndex);
            nextIndex++;
            return next;
        }

        @Override
        public boolean hasNext() {
            if (size < 0) {
                return false// empty list
            }
            if (nextIndex >= size) {
                return false// last element reached
            }

            return true;
        }

        @Override
        public void remove() {
            throw new RuntimeException("not supported yet");
        }

        /**
         * Directory is valid if it exists, does not represent a file, and can
         * be read.
         */
        private static void validateDirectory(File d) {
            if (d == null) {
                throw new IllegalArgumentException("Directory should not be null.");
            }
            if (!d.exists()) {
                throw new IllegalArgumentException("Directory does not exist: "
                                                   + d);
            }
            if (!d.isDirectory()) {
                throw new IllegalArgumentException("Is not a directory: " + d);
            }
            if (!d.canRead()) {
                throw new IllegalArgumentException("Directory cannot be read: "
                                                   + d);
            }
        }
    }

    private FileUtils() {
    }

    public static void deleteRecursively(File f) {
        if (!f.exists()) {
            return;
        }

        if (f.isDirectory()) {
            File[] children = f.listFiles();
            if (children != null) {
                for (int i = 0; i < children.length; i++) {
                    deleteRecursively(children[i]);
                }
            }
        }
        f.delete();
    }
}

   
    
    
    
    
  
Related examples in the same category
1.Get classpath using System class
2.Returns a reference to a file with the specified name that is located somewhere on the classpath.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.