| | |
Add Files to a Collection: pseudocode included inside
![]() |
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
ArrayList arrayList = new ArrayList();
for (File g : f.listFiles())
if (g.isFile())
{
arrayList.add(g);
}
I hope from the code above you can understand what Im trying to do. If a file is a file then add it to an arraylist. I dont know if this is the best one, its the one I know most. After adding it to an array, collection etc I would like to further process the files. But all I want help with is being able to extract the files from the list, eg all files that have equal size. So how can I go about this using my code as the basis? I done:
System.out.println (arrayList.toArray());
But that printed off some object references rather than filenames
So is arraylist not appropirate? and if so how can go about processing the files. eg listing all the files in the array that are equal in size
for (File g : f.listFiles())
if (g.isFile())
{
arrayList.add(g);
}
I hope from the code above you can understand what Im trying to do. If a file is a file then add it to an arraylist. I dont know if this is the best one, its the one I know most. After adding it to an array, collection etc I would like to further process the files. But all I want help with is being able to extract the files from the list, eg all files that have equal size. So how can I go about this using my code as the basis? I done:
System.out.println (arrayList.toArray());
But that printed off some object references rather than filenames
So is arraylist not appropirate? and if so how can go about processing the files. eg listing all the files in the array that are equal in size
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
ArrayList, from the info you have given, is a very good choice. Wen you declare and create it, specify the type of object it's supposed to contain - this will allow the compiler to check your code better and will simplify subsequent processing of its contents.
ArrayList<File> arrayList = new ArrayList<File>(); Now you will a;so be able to loop thru it using the improved loop syntax that you used with listFiles(), eg: java Syntax (Toggle Plain Text)
for (File f : arrayList ) { if (f.length() <2048) System.out.println(f.getName()); }
Last edited by JamesCherrill; May 23rd, 2009 at 6:08 am.
•
•
Join Date: Nov 2008
Posts: 37
Reputation:
Solved Threads: 0
thanks james, the code works fines with the rest of my code, but to be honest i dont really understand what difference it made, because the final results when executing my program are the same as to when I didnt explicity create an ArrayList.
What I was hoping it would do was enable me to ready binary data of files in two folders at a parallel level in the folder arcitecture.
Because at the moment, Im able to compute files from say Folder A and Folder B, so files in Folder A & B are computed independtly. I want to be able to put files from Folder A and files from Folder B into an arrayList X, and then I want to be able to compute the files in X as if they were in one folder and i could listFiles().
I dont know if ive conveyed what im trying to gt at, if some ones got a little free time and is able to review the code ill be happy to PM it.
What I was hoping it would do was enable me to ready binary data of files in two folders at a parallel level in the folder arcitecture.
Because at the moment, Im able to compute files from say Folder A and Folder B, so files in Folder A & B are computed independtly. I want to be able to put files from Folder A and files from Folder B into an arrayList X, and then I want to be able to compute the files in X as if they were in one folder and i could listFiles().
I dont know if ive conveyed what im trying to gt at, if some ones got a little free time and is able to review the code ill be happy to PM it.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
The addition of the <File> won't make a dramatic difference, but it will simplify processing the data (eg you won't have to do a runtime cast every time you access a member of the list).
Putting both directories into one ArrayList won't tax you too hard... just create a new ArrayList, add all the files from directory1, then add all the files from directory2.
Putting both directories into one ArrayList won't tax you too hard... just create a new ArrayList, add all the files from directory1, then add all the files from directory2.
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
You can use pmalhotra's error-prone difficult version with the unchecked run-time cast:
or, if you have Java 1.5 or later you can use the enhanced for loop
I know which I prefer.
Java Syntax (Toggle Plain Text)
for(int i=0; i<arrayList.size();i++){ File n= (File)arrayList.get(i); System.out.println(n.length()); }
or, if you have Java 1.5 or later you can use the enhanced for loop
Java Syntax (Toggle Plain Text)
for (File n : arrayList) { System.out.println(n.length()); }
I know which I prefer.
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Hate to ask but am wits end--HJT Log included (Viruses, Spyware and other Nasties)
- explorer.exe problem - Keeps restarting (Viruses, Spyware and other Nasties)
- Linking 2 files. (C)
- new window in Internet Explorer is always blank (Web Browsers)
- Open Image Files :: Autoclose :: HijackThis Log Included (Viruses, Spyware and other Nasties)
- Error with manifest resource files (ASP.NET)
- Delete files on restart (Windows NT / 2000 / XP)
- problems wid cpp files (C++)
- Error Linking KeyLogger.exe (C++)
Other Threads in the Java Forum
- Previous Thread: Bus Information System
- Next Thread: Need titlein title bar to be removed
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows






