I have something bugs

Discussion in 'Java' started by alex.korsak@gmail.com, Jul 3, 2007.

  1. Guest

    Hello. I have something bugs, i am dummy in java.

    Source:
    import java.io.File;
    import java.io.FilenameFilter;

    public class Main {
    public static void main(String[] args) {
    File f = new File("C:\\Windows");
    String[] strings = f.list( new FilenameFilter() { // throw exception
    public boolean accept(File arg0, String arg1) {
    if (new File(arg0.getAbsolutePath()).isDirectory())
    return true;

    return false;
    }
    });
    for (String value : strings) {
    System.out.println( value );
    }
    }
    }

    simple scan directory. i must printing name of directory.
    but this code is printing all files and directories in C:\Windows\
     
    , Jul 3, 2007
    #1
    1. Advertising

  2. Oliver Wong Guest

    <> wrote in message
    news:...
    > Hello. I have something bugs, i am dummy in java.
    >
    > Source:
    > import java.io.File;
    > import java.io.FilenameFilter;
    >
    > public class Main {
    > public static void main(String[] args) {
    > File f = new File("C:\\Windows");
    > String[] strings = f.list( new FilenameFilter() { // throw exception
    > public boolean accept(File arg0, String arg1) {
    > if (new File(arg0.getAbsolutePath()).isDirectory())
    > return true;
    >
    > return false;
    > }
    > });
    > for (String value : strings) {
    > System.out.println( value );
    > }
    > }
    > }
    >
    > simple scan directory. i must printing name of directory.
    > but this code is printing all files and directories in C:\Windows\


    Read the javadocs for FilenameFilter:

    http://java.sun.com/javase/6/docs/api/java/io/FilenameFilter.html

    - Oliver
     
    Oliver Wong, Jul 3, 2007
    #2
    1. Advertising

  3. kaldrenon Guest

    You want it to print all directories and skip files? I would do it
    this way:

    File f = new File("C\\Windows");
    File[] allItems = f.listFiles();
    for(File cur : allItems){
    if(cur.isDirectory()){
    System.out.println(cur.toString();
    }
    }

    -Andrew
     
    kaldrenon, Jul 3, 2007
    #3
  4. Guest

    > - Oliver

    Ups)) thx.

    I am dummies ))
    thx, thx
     
    , Jul 3, 2007
    #4
  5. Guest

    > - Oliver

    Ups)) thx.

    I am dummies ))
    thx, thx
     
    , Jul 3, 2007
    #5
  6. On Jul 3, 11:12 am, "" <>
    wrote:
    > Hello. I have something bugs, i am dummy in java.
    >
    > Source:
    > import java.io.File;
    > import java.io.FilenameFilter;
    >
    > public class Main {
    > public static void main(String[] args) {
    > File f = new File("C:\\Windows");
    > String[] strings = f.list( new FilenameFilter() { // throw exception
    > public boolean accept(File arg0, String arg1) {
    > if (new File(arg0.getAbsolutePath()).isDirectory())
    > return true;
    >
    > return false;
    > }
    > });
    > for (String value : strings) {
    > System.out.println( value );
    > }
    > }
    >
    > }
    >
    > simple scan directory. i must printing name of directory.
    > but this code is printing all files and directories in C:\Windows\


    The logic in accept( ) is incorrect. You need to check for the full
    file, and not just the parent folder. To get the full file name, you
    need to join the directory (arg0) and the file (arg1) with the
    separator character.

    Change the logic to:

    if (new File(arg0.getAbsolutePath()+File.separator
    +arg1).isDirectory())
    return true;

    and it should work.

    -cheers,
    Manish
     
    Manish Pandit, Jul 3, 2007
    #6
  7. Guest

    > - Oliver

    Ups)) thx.

    I am dummies ))
    thx, thx
     
    , Jul 3, 2007
    #7
  8. Guest

    > - Oliver

    Ups)) thx.

    I am dummies ))
    thx, thx
     
    , Jul 3, 2007
    #8
  9. Lars Enderin Guest

    Re: I have some bug(s)

    Manish Pandit skrev:
    > On Jul 3, 11:12 am, "" <>
    > wrote:
    >> Hello. I have something bugs, i am dummy in java.
    >>
    >> Source:
    >> import java.io.File;
    >> import java.io.FilenameFilter;
    >>
    >> public class Main {
    >> public static void main(String[] args) {
    >> File f = new File("C:\\Windows");
    >> String[] strings = f.list( new FilenameFilter() { // throw exception
    >> public boolean accept(File arg0, String arg1) {
    >> if (new File(arg0.getAbsolutePath()).isDirectory())
    >> return true;
    >>
    >> return false;
    >> }
    >> });
    >> for (String value : strings) {
    >> System.out.println( value );
    >> }
    >> }
    >>
    >> }
    >>
    >> simple scan directory. i must printing name of directory.
    >> but this code is printing all files and directories in C:\Windows\

    >
    > The logic in accept( ) is incorrect. You need to check for the full
    > file, and not just the parent folder. To get the full file name, you
    > need to join the directory (arg0) and the file (arg1) with the
    > separator character.
    >
    > Change the logic to:
    >
    > if (new File(arg0.getAbsolutePath()+File.separator
    > +arg1).isDirectory())
    > return true;
    >


    You don't need to construct the full file path explicitly if you use the
    File(String, String) constructor.
     
    Lars Enderin, Jul 3, 2007
    #9
  10. Roedy Green Guest

    On Tue, 03 Jul 2007 11:12:34 -0700, ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    >public class Main {
    > public static void main(String[] args) {
    > File f = new File("C:\\Windows");
    > String[] strings = f.list( new FilenameFilter() { // throw exception
    > public boolean accept(File arg0, String arg1) {
    > if (new File(arg0.getAbsolutePath()).isDirectory())
    > return true;
    >
    > return false;
    > }
    > });
    > for (String value : strings) {
    > System.out.println( value );
    > }
    > }
    >}


    Let's tidy it up:

    public class TestFilters {
    public static void main(String[] args) {
    File winDir = new File("C:\\Windows");
    String[] subDirNames = winDir.list( new FilenameFilter() {
    // accept dirs only.
    public boolean accept(File dir, String filename) {
    {
    return new File( dir, filename).isDirectory();
    }
    });

    for (String subDirName: subDirNames)
    {
    System.out.println( subDirName);
    }
    }

    The main thing I did was give each variable a clear name as to what it
    contained. Without that, programs become needlessly confusing.

    The other thing I did was get rid of your "absolute" call. You only
    need that when you want the precise file names. They are not needed
    to determine if a file is a directory.

    Further you the file you are testing has two parts the dir and the
    filename. You need to create a file handle to test that includes both
    parts.

    I am impressed you mastered the delicate syntax of the inner anonymous
    class. You are no dummy, just one who has not yet learned the
    importance of naming.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Jul 3, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brent
    Replies:
    0
    Views:
    371
    Brent
    Oct 26, 2005
  2. ad
    Replies:
    1
    Views:
    358
    Jim Cheshire
    Apr 1, 2006
  3. Guest
    Replies:
    4
    Views:
    502
    Guest
    Oct 13, 2004
  4. Pekka Järvinen
    Replies:
    2
    Views:
    692
    Richard Tobin
    Apr 29, 2008
  5. Josef 'Jupp' Schugt

    Still use 'ruby-bugs' for Ruby bugs?

    Josef 'Jupp' Schugt, Nov 4, 2004, in forum: Ruby
    Replies:
    2
    Views:
    175
    Tom Copeland
    Nov 4, 2004
Loading...

Share This Page