Results 1 to 5 of 5
  1. #1
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default java isDirectory() is not detecting that my folders are folders!

    hi all, somehow the "isDirectory()" function is not saying that my folders are indeed folders. I bascially just iterate throught my list of files and check if they are files are folders. it turns out that it just say everything in there are folders.

    has anyone got any problems like this before?

    I did a very simple check and it didn't work, so i tried this method and it did work, but it fails on files suchs as "changelog" files. Problem is becuase it's not saying the file "changelog" is a file, but rather a folder.


    Java Code:
    .
    .
    .
    
                            File f1 = new File("folderone");
                            String[] f1list = f1.list();
                            for(int x = 0 ; x < f1list.length; x++){
                                File f1temp = new File(f1list[x]);
                                
                                if(f1temp.isFile() || hasDot(f1temp.getName())){
                                    System.out.println("file: " + f1list[x]);
                                }
                                if(!f1temp.isFile() && f1temp.isDirectory() && !hasDot(f1temp.getName())){
                                    System.out.println("folder: " + f1temp);
                                    
    
                                }
                            }
    .
    .
    .
    
            public static boolean hasDot(String str){
                    for(int x = 0; x < str.length(); x++){
                        if(str.charAt(x) == '.'){
                            return true;
                        }
                    }
                return false;
            }
    .
    .
    .

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It's not clear what dots have to do with it.

    You could use listFiles() rather than list() and use a foreach loop to iterate over the returned array of File. Keep it simple: just call isDirectory() and isFile() respectively to determine whether you have a directory or (normal) file.

    If code reports that everything is a directory you should post that code because it's wrong. (except of course if there there are no nondirectories...)

  3. #3
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    awsome !!!!!! thanks

    Yeah i did some unesscessary work becuase it couldn't idetify weather it was a file or folder, but the listFile() worked out great!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    It has nothing - now - to do with your problem but the hasDot() business can also be simplified using a "builtin" method. String has an indexOf() method that will do this:

    Java Code:
    str.indexOf('.') != -1; // true if and only if there is a dot

  5. #5
    seandingobat is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    oh coool. that would save TONS of time! I've only beening using java for about 5 months now on and off from time to time. I got so sick of looking for backup software to back up my files so i'm just taking my time writing up some simple codes that mirrors a folder to a folder (or drive to drive). I should just be using Raid0 to do this, but i though it'd be fun! hahaha.

    thanks again!

Similar Threads

  1. Configuring Java source folders for eclipse
    By kfir.wolfson@gmail.com in forum Eclipse
    Replies: 8
    Last Post: 10-01-2010, 12:11 PM
  2. Lots of my folders have become read-only
    By Manoeuvre in forum Eclipse
    Replies: 5
    Last Post: 12-02-2008, 12:48 PM
  3. Folders instead of packages after OS crashed
    By Kubuntek in forum Eclipse
    Replies: 0
    Last Post: 08-03-2008, 02:26 PM
  4. Replies: 20
    Last Post: 12-17-2007, 03:20 PM
  5. URGENTTT...zip folders in java
    By rach in forum New To Java
    Replies: 3
    Last Post: 08-13-2007, 03:55 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
  •