Results 1 to 6 of 6

Thread: Counting file extension

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Counting file extension

    Hello,
    I have a problem and I have no solution for this. I have a folder of data whose relative address is well known for my java application running. And I absolutely have to count the number of files that have a specified extension ex. the *. properties and retrieve all their names without the extension. I do not know how to do it. Can anyone help with this.
    Last edited by Vodka; 23-01-2010 at 02:28 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Counting file extension

    Hello,
    I have a code with me, see if this helps you
    Code:
    File fl = new File("Path of directory file properties");
      File[] prls = fl.listFiles(new FileFilter() {
       Public boolean accept(File file) {
        if (file.getName().endsWith(. "properties"))
         return true;
        return false;
       }
      });

  3. #3
    Join Date
    Dec 2009
    Posts
    192

    Re: Counting file extension

    Hello,
    This is my program, just have a look at it.
    Code:
    Public class CompteurDeFichiersAvecExtension
    {
        Public static void hand(String[] args)
        {
            File fl = new File(. / datas ");
            File[] prls = fl.listFiles(new FileFilter()
                    {
                    Public boolean accept(File file)
                      {
                      if ((file.isDirectory()==false) &&
                          (file.getName().endsWith(. "properties")))
                             {
                             return true;
                             }
                      return false;
                      }
                    });
            for(int i =0; I <prls.length; I + +)
            {
                String = nomAvecExt prls[i].getName();
                int lg = nomAvecExt.length();
                String nm = nomAvecExt.substring(0, Long-11);
                System.out.System.out.println(nm);
            }
        }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Counting file extension

    Hello,
    Check out this code, I think it can help you.
    Code:
    Public class CompteurDeFichiersAvecExtensionG
    {
        Public static void hand( Final String[] args)
        {
            File fl = null;
            File[] prls = null;
            String fld = null;
            String fil = null;       
            if ( args.length == 0 ) { fld = "."; Filter = ""; }
            else if ( args.length == 1 ) { fld = "."; Filter = args[0]; }
            else { fld = args[0]; Filter = args[1]; }
            
            fl = new File(fld);
            
            if (( ! fil.startsWith(".") )&&( args.length != 0 )) fil = "."+ fil;        
    
            Final String tmp = fil;
            
            prls = fl.listFiles(new FileFilter()
                    {
                    Public boolean accept(File file)
                      {
                      if ((file.isDirectory()==false) &&
                          (file.getName().endsWith(tmp)))
                             {
                             / / Only files are made, not
                             / / The directory.
                             return true;
                             }
                      return false;
                      }
                    });
    
            for(int i =0; I <prls.length; I + +)
            {
                String = nomAvecExt prls[i].getName();
                int lg = nomAvecExt.length();
                String nomSansExt = nomAvecExt.substring(0, Long-fil.length());
                System.out.System.out.println(nomSansExt);
            }
            
            System.out.System.out.println("\ n > "+ prls.length+"File *"+ + fil"Found.");
        }
    }

  5. #5
    Join Date
    Dec 2009
    Posts
    192

    Re: Counting file extension

    Hello,
    I have a small question, How to catch this error if the folder is not referenced.
    Code:
    fl = new File (folder);
    NullPointerException in thrown? Yet folder String is never empty. If the program does not find the folder, it crashes. So, I think I should mention a try and catch block in my code.

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: Counting file extension

    Hi,
    You can make your code more reliable, by just including the following code.
    Code:
    File fl = new File(. / data ");
    if (fl.exists()) {
        doSomething();
    } else {
        doNothing();
    }
    If you have any more problems in this then do post by with your query.

Similar Threads

  1. file extension .sig
    By Clay Aiken in forum Windows Software
    Replies: 4
    Last Post: 16-04-2009, 11:54 PM
  2. file extension?
    By Jack P in forum Windows Vista Mail
    Replies: 2
    Last Post: 15-04-2009, 01:59 AM
  3. .ebu file extension
    By Samra in forum Operating Systems
    Replies: 2
    Last Post: 13-04-2009, 11:51 PM
  4. Replies: 1
    Last Post: 22-01-2009, 07:36 PM
  5. Replies: 0
    Last Post: 20-06-2008, 11:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,060,589.50369 seconds with 16 queries