Results 1 to 20 of 21
- 12-11-2007, 10:41 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
can java.io.File create a list of all files and folders.
Hello,
My goal is to create a app that will take the folder passed to it and spit out a txt file with a list of all the files and folders with last modify date.
Example,
C:\<TAB>12/1/2007 1:00 PM
C:\config.sys<TAB>12/1/2007 1:00 PM
C:\Windows\<TAB>12/1/2007 1:00 PM
C:\windows\test.txt<TAB>12/1/2007 1:00 PM
C:\windows\Program Files\<TAB>12/1/2007 1:00 PM
C:\windows\Program Files\test.txt<TAB>12/1/2007 1:00 PM
I have played with several options from parsing a dos output, and java.io.File list /w for loop and if isDirectory. The dos output may work if I get the time and patience to tweak it and the java.io.File is a better possibility.
I would like to use java.io.File to do this the issue that I am running is it only takes the current directory and not next one. I could rap it in a method and call on every directory would this pause untill the new folder is complete? Does that make since, LoL?
Please tell me there is a easier way....Java Code:public FolderList(String str) { File b = new File(str); String [] ch = b.list(); path = str; for (int i = 0; i < ch.length; i++){ File c = new File(path + ch[i]); if (c.isDirectory()){ System.out.println(path + ch[i] + "\\"); } else System.out.println(path + ch[i]); } }
In VB6 I did something similar and used a FileBox thingy and displayed path and filename in txt box then sent to list box (list box has a max # of items, ugh!!! :D Java is better anyway :D .)
Now that I have effectivly wasted space, can you please help a little... you don't have to do the code, I would just like guidance and maybe simplistic example.
YOU ROCK...Thanks is advance, i'll shut up now.... this is so much fun, I'm going to learn more and more... thanks.:) :) :) :)
- 12-12-2007, 01:52 AM #2
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Here is one way to do it:
Java Code:public void iterate(File folder) { ArrayList<File> folders = getFolders(folder); for (int i = 0; i < folders.size(); i++) { System.out.println(folders.get(i)); // we add the subfolders of the current folder to the main FOLDERS array list folders.addAll(getFolders(folders.get(i))); // then we remove the current folder, so that we dont read it twice folders.remove(i); } } private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); // uses FileFilter to just pull out directories, then puts in an array File[] list = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) return true; return false; } }); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) listAr.add(list[i]); return listAr; }Last edited by staykovmarin; 12-12-2007 at 01:56 AM.
- 12-12-2007, 02:38 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
??????
OK I think I get the rest, get this and not sure what todo.
:confused:The method accept(File) of type new FileFilter(){} must override a superclass method
and just to confirm for test.
:DJava Code:public static void main(String[] args) { FolderList myFolderList = new FolderList(); File File = new File("C:\\"); myFolderList.iterate(File);
- 12-12-2007, 07:18 PM #4
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
As you can see in those few lines i AM creating a FileFilter, in this portion specifically:Java Code:File[] list = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) return true; return false; } });
Is where i override accept(File file).Java Code:@Override public boolean accept(File file) {
And yeah, thats the right way to use it, calling iterate will loop trough all the folders, printing them to command line.
- 12-12-2007, 07:31 PM #5
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
????
:confused: I will Show you what I have.... LoL...
Java Code:package folderList; import java.io.File; import java.io.FileFilter; import java.util.ArrayList; public class FolderList { public void iterate(File folder) { ArrayList<File> folders = getFolders(folder); for (int i = 0; i < folders.size(); i++) { System.out.println(folders.get(i)); // we add the subfolders of the current folder to the main FOLDERS array list folders.addAll(getFolders(folders.get(i))); // then we remove the current folder, so that we dont read it twice folders.remove(i); } } private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); // uses FileFilter to just pull out directories, then puts in an array File[] list = folder.listFiles(new FileFilter() { @Override public boolean accept(File file) { if (file.isDirectory()) return true; return false; } }); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) listAr.add(list[i]); return listAr; } public static void main(String[] args) { FolderList myFolderList = new FolderList(); File File = new File("C:\\"); myFolderList.iterate(File); } }
Eclipse Error...
Is it not working because something I am not doing....?Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method accept(File) of type new FileFilter(){} must override a superclass method
at folderList.FolderList.getFolders(FolderList.java:2 5)
at folderList.FolderList.iterate(FolderList.java:10)
at folderList.FolderList.main(FolderList.java:43)
:confused:
- 12-12-2007, 07:51 PM #6
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
What version of java are you running?
- 12-12-2007, 08:42 PM #7
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
???
Eclipse SDK
Version: 3.2.2
Build id: M20070212-1330
JRE1.5.0_10
????downloading new Eclipse, maybe it has updates.
Java(tm) Platform, Standard Edition 6
Version 1.6.0 (build 1.6.0_03-b05)Last edited by MattStone; 12-12-2007 at 08:58 PM.
- 12-12-2007, 09:02 PM #8
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Try removing the @Override
- 12-12-2007, 09:08 PM #9
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
???
by the way thank you for the help.....C:\765cb90a07417be0f07777bf99b52d
C:\ATI
C:\Bootable CD Project
C:\downloads
C:\eclipse
C:\ListTemplates.xml
C:\MSOCache
C:\notes
C:\QUARANTINE
C:\Rename
C:\System Volume Information
Exception in thread "main" java.lang.NullPointerException
at folderList.FolderList.getFolders(FolderList.java:3 3)
at folderList.FolderList.iterate(FolderList.java:14)
at folderList.FolderList.main(FolderList.java:43)
- 12-12-2007, 09:51 PM #10
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Hmm the problem right now is that you are scanning files as well, so the FileFilter is not working. I have no idea why its not, unless maybe you dont have your eclipse actually using java 5+ for a compiler: Window > Prefferences > Java > Compiler, select 5 or 6 from the drop down.
Here is a revised code that doesnt use a FileFilter: (note: i am not compiling this, because i dont have java atm, but it should work)
Java Code:private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); File[] list = folder.listFiles(); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) { if (listAr.add(list[i]).isDirectory()) listAr.add(list[i]); } return listAr; }
- 12-12-2007, 09:57 PM #11
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
it is set to 5..
6 does not work dont think i have right files in this version.
just finished dl on new one ill let you know. then i will test you new code.
- 12-13-2007, 03:14 PM #12
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
OK I guess my other version of eclipse was old....
runs.... not sure if it completes but it does not error.. LoL.
Sorry for the issues I should have been on the latest version, didn't relize how dependent it might be. Learning alot YAY!
now for the questions and insanity check......LoL.
----
Let me see if I understand what you are doing.
Order of Operation not order of code...
you define the getFolders method
create a memory space
create a memory space for FileFilter
redefining what FileFilter sees by Overriding accept to only directories? (meaning only allow folders, don't care about files)
check if directory
return true or false (accept the result or don't show it)
then store directories (if true) in array (because FileFilter only allows Folders, thats all you have)
now that you have the method getFolders
use the iterate method to execute
create a memory space using a array for getFolders method
loop array to get next item
print line to screen
get folders for current folder
then remove current
Looks like this works in a small area and in large areas memory would fill up(???). Could you combine these steps and make it just hold the next item (or manual counters in a for loop) and not store in array or does it have to use array?Last edited by MattStone; 12-13-2007 at 03:27 PM.
- 12-13-2007, 03:19 PM #13
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
listAr.add(list[i]).isDirectory()Java Code:private ArrayList<File> getFolders(File folder) { ArrayList<File> listAr = new ArrayList<File>(); File[] list = folder.listFiles(); // puts all the items from the array in an arraylist for (int i = 0; i < list.length; i++) { if (listAr.add(list[i]).isDirectory()) listAr.add(list[i]); } return listAr; }
----Cannot invoke isDirectory() on the primitive type boolean
- 12-13-2007, 10:03 PM #14
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Sorry, i didnt get a chance to run that code, so i missed up that part:
change that to :Java Code:if (listAr.add(list[i]).isDirectory())
Edit:Java Code:if (list[i].isDirectory())
I didnt see your other post
getFolders returns an Arraylist of all the subfolders of the given folder. So what we do is combine the original folders ArrayList with each directories subfolders. Then we remove the folder that was scanned, so we dont scan againyou define the getFolders method
create a memory space
create a memory space for FileFilter
redefining what FileFilter sees by Overriding accept to only directories? (meaning only allow folders, don't care about files)
check if directory
return true or false (accept the result or don't show it)
then store directories (if true) in array (because FileFilter only allows Folders, thats all you have)
now that you have the method getFolders
use the iterate method to execute
create a memory space using a array for getFolders method
loop array to get next item
print line to screen
get folders for current folder
then remove currentLast edited by staykovmarin; 12-13-2007 at 10:15 PM.
- 12-13-2007, 10:20 PM #15
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
Thank no errors now. works.
please read post #12 offer any input?
again thanks for all your help.
- 12-14-2007, 07:22 PM #16
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
I did as I thought and BAM!......sweetness, a noob can be good at something. LoL.
Only one Array......File[]
Now how can I get a "TAB" and "last modify date" in the results? That possible? (When I copy list to excel I want column 1 folder column 2 modify date) Next step will be to get all files and folders...LoL. oooo lots of possibilities once I can do that. LoL.
Changed: to allow folders and files... that was easy once I thought about it.. took a break and then got back to it... LoL.
Changed: to allow Tab and "Last modify date" OK OK think I got what I need.... see anything that may cause issues?
Error found???? i guess the one array is filling up????? maybe write to txt and delete could fix???? any suggestions??????
Java Code:package folderList; import java.io.File; import java.util.Date; public class FolderList { public void ListIt(File folder) { File[] list1 = folder.listFiles(); for (int i = 0; i < list1.length; i++) { long modTime = list1[i].lastModified(); Date d = new Date(modTime); if (list1[i].isDirectory()) { FolderList list2 = new FolderList(); list2.ListIt(list1[i]); System.out.println(list1[i] + "\t" + d.toString()); } else System.out.println(list1[i].toString() + "\t" + d.toString()); } } public static void main(String[] args) { FolderList myFolderList = new FolderList(); File File = new File("C:\\Documents and Settings\\<UserName>\\Desktop\\Copy of #Test#Test"); //File File = new File("C:\\"); myFolderList.ListIt(File); } }Last edited by MattStone; 12-14-2007 at 08:50 PM.
- 12-14-2007, 09:19 PM #17
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
I am not sure why you are even doing that. i would do something like this:Java Code:System.out.println(list1[i] + "\t" + d.toString());
What error are you getting?Java Code:System.out.println(list1[i] + "\t" + list1[i].lastModified());
- 12-14-2007, 09:24 PM #18
Member
- Join Date
- Dec 2007
- Posts
- 13
- Rep Power
- 0
System.out.println(list1[i].lastModified());
prints a mili second string, didnt see how to format it.
1083077609416
Date d = new Date(modTime);
System.out.println(d.toString());
Fri Aug 10 14:41:29 EDT 2007
EDIT: seems to work now... do you see any issues that I might not?
EDIT: Changed package and file name.
EDIT: Seems to run into a issue.
Optional, I can figure error handeling out later. How can I capture this error and display something useful... like line the problem is on?
I just need to know why right now, is it something I am forgetting?
Exception in thread "main" java.lang.NullPointerException
at listIt.ListIt.getList(ListIt.java:18)
at listIt.ListIt.<init>(ListIt.java:12)
at listIt.ListIt.getList(ListIt.java:24)
at listIt.ListIt.<init>(ListIt.java:12)
at listIt.ListIt.main(ListIt.java:35)
Sorry I keep bugging you, you are helping me alot here. I keep playing as I can which is why you see the EDITs to keep current steps. I change the code to be the latest and error to follow just to make sure you understand. I am decent at understanding code I just lack overall knowlege and expierence I guess. Once I am done with this thing I will buy a book and try to learn more on my own. In the mean time thanks for the help. I have stayed true to my original request and just trying to get the final result. If you see me break any coding rules please let me know so I learn that part as well, I want others to be able to read it. You Rock!
Java Code:package listIt; import java.io.File; import java.util.Date; public class ListIt { public ListIt(String folder) { File File = new File(folder); this.getList(File); } private void getList(File folder) { File[] list1 = folder.listFiles(); for (int i = 0; i < list1.length; i++) { long modTime = list1[i].lastModified(); Date d = new Date(modTime); if (list1[i].isDirectory()) { ListIt list2 = new ListIt(list1[i].toString()); System.out.println(list1[i] + "\\\t" + d.toString()); } else System.out.println(list1[i].toString() + "\t" + d.toString()); } } public static void main(String[] args) { //ListIt myListIt = new ListIt("C:\\Documents and Settings\\"); ListIt myListIt = new ListIt("C:\\"); } }Last edited by MattStone; 12-14-2007 at 11:15 PM.
- 12-15-2007, 01:13 AM #19
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
That should work okay for youJava Code:import java.io.File; import java.util.ArrayList; import java.util.Date; public class ListIt { public ListIt(String folder) { File File = new File(folder); this.getList(File); } private void getList(File folder) { ArrayList<File> folderList = new ArrayList<File>(); // first we add all the contents of the folder that we want to scan to // the folderList folderList.addAll(getSubs(folder)); // then we iterrate trough the folder list for (int i = 0; i < folderList.size(); i++) { if (folderList.get(i).isDirectory()) { // if its a directory, we get all its subfolders/files and add them to the folderList folderList.addAll(getSubs(folderList.get(i))); } // then we print out the data: System.out.println(folderList.get(i) + "\tDate Modified:\t" + new Date(folderList.get(i).lastModified()).toString()); } } // gets the sub files of a folder public ArrayList<File> getSubs(File dest) { File[] subfiles = dest.listFiles(); ArrayList<File> subFolders = new ArrayList<File>(); // if subfiles is null, it means that listFiles() didnt return anything, // so we are dealing with a file, not a folder if (subfiles != null) // all teh contents of the folder are added to the array list for (int i = 0; i < subfiles.length; i++) { subFolders.add(subfiles[i]); } else // otherwise we jist add the file to the list subFolders.add(dest); return subFolders; } public static void main(String[] args) { // ListIt myListIt = new ListIt("C:\\Documents and Settings\\"); ListIt myListIt = new ListIt("C:\\"); } }
- 12-15-2007, 02:44 AM #20
Senior Member
- Join Date
- Nov 2007
- Location
- Newport, WA
- Posts
- 141
- Rep Power
- 0
Might want to change the for loop a little:
Java Code:for (int i = 0; i < folderList.size(); i++) { if (folderList.get(i).isDirectory()) { // if its a directory, we get all its subfolders/files and add them to the folderList folderList.addAll(getSubs(folderList.get(i))); } // then we print out the data: System.out.println(folderList.get(i) + "\tDate Modified:\t" + new Date(folderList.get(i).lastModified()).toString()); // remove the current folder from the list, since we dont need it anymore folderList.remove(i); }
Similar Threads
-
How to create a Sorted List in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:31 PM -
JAR file with .java files
By ravian in forum New To JavaReplies: 7Last Post: 12-24-2007, 08:15 PM -
How do you recursively traverse through file folders ?
By nimraj in forum New To JavaReplies: 2Last Post: 11-27-2007, 01:45 PM -
URGENTTT...zip folders in java
By rach in forum New To JavaReplies: 3Last Post: 08-13-2007, 03:55 PM -
Create view of files in java-Swing
By Albert in forum AWT / SwingReplies: 1Last Post: 07-06-2007, 06:06 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks