We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I was just wondering how I would approach loading text files and images from different directories in processing? I tried using loadImage() and loadTable(), which only works if the files are in the sketch folder's root directory or a data folder inside the sketch folder. It doesn't seem to work if I have different folders inside sketch folder. They say the files do not exist.
Thank you
Answers
You can create folders in "data" and acess them as usual, alternatively, you can use the absolute path, or use sketchPath() and dataPath() to get path to these folders. See this thread.
For "images/":
String folder = sketchPath("images/");
For "data/images/":
String folder = dataPath("images") + '/';
Thank you!!
GoToLoop: would String folder = dataPath("images") + '/'; give me everything inside the "data/images" directory? I want to store the name of all the files in that folder into a string array to pass into the ControlP5 dropDownList.
Edit: I found this :
File [] files=new File("nameofdirectory").listFiles();
String filenames[]=new String[files.length];
for(int i=0; i<files.length; i++) {
filenames[i]=files[i].getAbsolutePath();
println(filenames[i]);
}
Is there a more efficient way or something processing specific instead of java?
You're gonna need dataFile("") instead to get a File object. Then call listFiles() over it:
http://docs.Oracle.com/javase/8/docs/api/java/io/File.html
Hi GoToLoop I get a NullPointerException from pics.length. Is it because its not grabbing the absolute path?
According to File::listFiles()'s reference:
http://docs.Oracle.com/javase/8/docs/api/java/io/File.html#listFiles--
Much probably you don't have that folder yet! 8-X
Just check out whether
pics == null
before going any further. :-@Another option is checking whether
folder.isDirectory()
: *-:)http://docs.Oracle.com/javase/8/docs/api/java/io/File.html#isDirectory--
That's weird. So i did File folder = dataFile("data2"); I do have that directory in my sketch folder.
edit: I found another thread from 2 years ago where you posted: "Processing isn't aware of its own save folder before setup() begins! You should initialize such classes only after setup() started!"
I have been doing the files outside of setup haha. You mean I would have to do that in setup but I can leave the declaration outside?
Pay attention that dataFile("") refers to "sketchFolder/data/" folder.
Therefore
dataFile("data2");
should result in: "sketchFolder/data/data2" folder.Pay attention at what you had marked as answer: :-\"
Ohh So then I would have to have those inside my setup Plus use sketchPath() instead right?
The choice among sketchPath(), sketchFile(), dataPath() & dataFile(), depends on how your files are organized inside the sketchFolder and whether you prefer a String or a File object! :-B
Okay that fixed it! getPath() returns the entire path like "/Users/Name/Desktop/sketch/data2/fileName". Is there a way to just get the fileName out or would I have to manually parse that part?
Edit: Nevermind, getName() does what I need.
Thanks!!!
Thanks for all the help GoToLoop!! You've been my savior these past two days haha!
Also File::list() returns String[] instead of File[]: :ar!
http://docs.Oracle.com/javase/8/docs/api/java/io/File.html#list--