We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › FileSystemReference and navigating directories
Page Index Toggle Pages: 1
FileSystemReference and navigating directories (Read 519 times)
FileSystemReference and navigating directories
Jan 18th, 2008, 1:06am
 
I'm not very comfortable working with the Java documentation on the web, so I haven't gotten far with using the FileSystemView class.

In trying to explore the use of the File class, I did the following:

import javax.swing.filechooser.FileSystemView;

void setup(){
 size(100,100);
 
 print( File.listRoots() );
 println( File.listRoots() );
}

Which outputs:
first line: [Ljava.io.File;@c1ea29second line: [0] /
(note: I'm not sure why the line doesn't break in the post)

I think the first is an object reference of some kind. If anyone has any pointers of how to use these classes to drive around the computer and get paths to files, I would appreciate it.

I am hoping to be able to have processing look in a directory and load all the images found therein. Currently, I am using sequentially-named images and using a for-loop to construct the sequence of filenames to load in using loadImage. I would like to bypass the need to sequence my images.
Re: FileSystemReference and navigating directories
Reply #1 - Jan 18th, 2008, 6:41am
 
To list the files of a directory, first instanciate a File object corresponding to that dir :
Code:
java.io.File dir = new java.io.File("c:/mydir"); 


Then, use the list() method to get all files in that directory :
Code:
File[] files = dir.listFiles(); 


Finally, iterate through this array of filenames :
Code:
for (int i = 0; i < files.length; i++) {
String path = files[i].getAbsolutePath();
if (path.toLowerCase().endsWith(".jpg")) {
println(path);
}
}
Re: FileSystemReference and navigating directories
Reply #2 - Jan 18th, 2008, 10:10pm
 
Thank you for the helpful response. I encountered some weirdness with the mac platform, which doesn't seem to allow me to enter absolute paths in java. Instead, all filepaths are relative to the Processing application directory.

Here is some code that works on mac osx (just collages the images in the folder):

import java.io.File;

PImage[] images;

void setup(){
 size(1024, 768);
 //note: the directory structure is relative to the processing application folder on a mac
 File dir = new File("../../Users/oxford/Documents/someImages");
 
 File[] files = dir.listFiles();
 images = new PImage[files.length];
 
 int numImages = 0;
 for( int i=0; i < files.length; i++ ){
   String path = files[i].getAbsolutePath();
   if( path.toLowerCase().endsWith(".jpg") || path.toLowerCase().endsWith(".png") ){
     images[i] = loadImage( path );
     numImages++;
   }
 }
 images = (PImage[])subset( images, 0, numImages );
 
 for( int i=0; i < images.length; i++ ){
   image( images[i], random( -images[i].width/2, width - images[i].width/2), random( -images[i].height/2, height - images[i].height/2));
 }
 
}
Page Index Toggle Pages: 1