Results 1 to 7 of 7
  1. #1
    corpusluteum is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default how to list certain files in a given directory in java

    hey all...
    I am a newbie in java and looking for how to list .java files in a given directory ( it should also include subdirectories ) and number of lines in that java files also should be listed. like that:

    in c:\temp

    tempp.java 22 line
    helloo.java 7 line

    etc.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default filbert the filedog

    Java Code:
    // This is one way to do it:
    File filbert = new File("C:\\temp");
    File[] fileList = filber.listFiles();
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    to list .java files in a given directory ( it should also include subdirectories ) and number of lines in that java files also should be listed
    Several parts to this.
    1) Use a filter object with the File list method to chose which files you want listed.
    2) To get subdirectories, you'll need a search method (often recursive ) to go into sub directorires
    3) To get the number of lines in a file, you'll have to read the file and count the number of newline characters.

  4. #4
    corpusluteum is offline Member
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    Now i m googleing how to use a filter object
    thanks much for the hints..

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    The file filter is a simple class that implements an interface. The interface has a method that is given the filename and returns a boolean. Read the API doc for the particulars.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.io.*;
    
    public class TraversingFiles {
        static int totalFiles = 0;
    
        public static void main(String[] args) {
            String path = ".";  // current directory
            File file = new File(path);
            System.out.println("parent folder = " + file.getName());
            File[] files = file.listFiles(new TextFilter());
            traverse(files);
            System.out.println("totalFiles = " + totalFiles);
        }
    
        private static void traverse(File[] files) {
            for(int i = 0; i < files.length; i++) {
                if(files[i].isDirectory()) {
                    System.out.println("____folder: " + files[i].getName() +
                                       "____");
                    traverse(files[i].listFiles(new TextFilter()));
                    continue;
                }
                int lines = getLines(files[i]);
                System.out.printf("%50s  %3d lines%n", files[i].getName(), lines);
                totalFiles++;
            }
        }
    
        private static int getLines(File file) {
            int count = 0;
            try {
                BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                    new FileInputStream(file)));
                while(br.readLine() != null) {
                    count++;
                }
                br.close();
            } catch(IOException e) {
                System.out.println("read error for " + file.getPath() +
                                   ": " + e.getMessage());
            }
            return count;
        }
    }
    
    class TextFilter implements FileFilter {
        String JAVA = "java";
        String TXT  = "txt";
        String HTML = "html";
    
        public boolean accept(File file) {
            if(file.isDirectory()) {
                return true;
            }
            String ext = getExtension(file);
            return ext.equals(JAVA) || ext.equals(TXT) || ext.equals(HTML);
        }
    
        private String getExtension(File file) {
            String s = file.getPath();
            int dot = s.lastIndexOf(".");
            if(dot > -1 && dot < s.length()) {
                return s.substring(dot+1);
            }
            return null;
        }
    }

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default

    return null;
    That will throw a NPE in
    ext.equals(JAVA)
    better to use
    JAVA.equals(ext)

Similar Threads

  1. how to find files in given directory
    By cecily in forum New To Java
    Replies: 7
    Last Post: 07-04-2014, 12:03 PM
  2. load all files in a directory
    By moomoo in forum New To Java
    Replies: 1
    Last Post: 04-21-2008, 10:18 AM
  3. Deleting an directory/subdirectory/files
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 07:18 AM
  4. Replies: 20
    Last Post: 12-17-2007, 03:20 PM
  5. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-14-2007, 11:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •