Reading textfiles/images from directory

edited February 2016 in Programming Questions

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

  • Answer ✓

    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.

  • Answer ✓

    For "images/": String folder = sketchPath("images/");
    For "data/images/": String folder = dataPath("images") + '/';

  • Thank you!!

  • edited February 2016

    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?

  • edited February 2016

    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

    // forum.Processing.org/two/discussion/14985/reading-textfiles-images-from-directory
    // GoToLoop (2016-Feb-18)
    
    File folder = dataFile("images");
    println(folder);
    
    File[] pics = folder.listFiles();
    println("# of files found at folder above:", pics.length, ENTER);
    printArray(pics);
    println();
    
    String[] filenames = new String[pics.length];
    for (int i = 0; i < pics.length; filenames[i] = pics[i++].getPath()); 
    
    println(filenames);
    exit();
    
  • Hi GoToLoop I get a NullPointerException from pics.length. Is it because its not grabbing the absolute path?

  • edited February 2016 Answer ✓

    According to File::listFiles()'s reference:
    http://docs.Oracle.com/javase/8/docs/api/java/io/File.html#listFiles--

    If this abstract pathname does not denote a directory, then this method returns null.

    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--

  • edited February 2016

    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?

  • edited February 2016 Answer ✓

    So i did File folder = dataFile("data2"); I do have that directory in my sketch folder.

    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: :-\"

    For "images/": String folder = sketchPath("images/");
    For "data/images/": String folder = dataPath("images") + '/';

  • Ohh So then I would have to have those inside my setup Plus use sketchPath() instead right?

  • edited February 2016 Answer ✓

    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

  • edited February 2016

    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!

  • edited February 2016

    Also File::list() returns String[] instead of File[]: :ar!
    http://docs.Oracle.com/javase/8/docs/api/java/io/File.html#list--

Sign In or Register to comment.