Results 1 to 5 of 5
- 10-09-2009, 10:42 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
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; } . . .
- 10-09-2009, 11:14 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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...)
- 10-09-2009, 11:25 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
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!
- 10-09-2009, 11:57 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
- 10-10-2009, 12:26 AM #5
Member
- Join Date
- Sep 2008
- Posts
- 16
- Rep Power
- 0
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
-
Configuring Java source folders for eclipse
By kfir.wolfson@gmail.com in forum EclipseReplies: 8Last Post: 10-01-2010, 12:11 PM -
Lots of my folders have become read-only
By Manoeuvre in forum EclipseReplies: 5Last Post: 12-02-2008, 12:48 PM -
Folders instead of packages after OS crashed
By Kubuntek in forum EclipseReplies: 0Last Post: 08-03-2008, 02:26 PM -
can java.io.File create a list of all files and folders.
By MattStone in forum New To JavaReplies: 20Last Post: 12-17-2007, 03:20 PM -
URGENTTT...zip folders in java
By rach in forum New To JavaReplies: 3Last Post: 08-13-2007, 03:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks