Writing a finding a file in a directory

Hi, I have a project that needs to find a file in a directory that is set by the user, at the moment the file simply sits in the main sketch directory for testing purposes. I am using the following code (Hopefully the code will formatted properly - I don't think its working) ;

void findFile() {
    //File f = new File(dataPath("4-3-2014.txt")); //dataPath finds path of file in sketch folder
    File f = new File(dataPath(dateString));
    if (!f.exists()) {
        println("File does not exist");
        found = false;
    }
    else {
        found = true;
    }
}

However this doesn't seem to work - all I need to know is whether the fiel exists or not.

The other query I have is how to write a file to a user defined directory. I am currently using this code;

try {
    dataFile = new FileWriter(dateString,true); //the true will append the new data
    dataFile.write("Multi Seial logger V2\n");
    dataFile.flush();
    //dataFile.close();
  }
  catch(IOException e) {
    println("It Broke : / ");
    e.printStackTrace();
  }

But this defaults to putting the file in the main processing directory - how do I put it elsewhere?

Any help is much appreciated.

Cheers.

Answers

  • Answer ✓

    "the file simply sits in the main sketch directory"
    dataPath() gives the path to the data folder in the sketch folder. Use sketchPath() for finding your file, instead.

    "how to write a file to a user defined directory"
    dateString must have the full path to this directory.

Sign In or Register to comment.