Checking to see if a file exists

edited April 2015 in Arduino

I'm using Processing along with an Arduino sketch to help me save data from a sensor. However, I'm trying not to write that data to a text file (hold.txt) until another text file (record.txt) is created. I'm using exists() but it isn't working. Any help would be greatly appreciated! Here's parts of my code...

        PrintWriter output;
        File f = new File(dataPath("record.txt"));
        .......

        void setup() {
           output = createWriter("hold.txt");
        .......

        void serialEvent(Serial port) {
           while(port.available() > 0) {
               int ch = port.read();
           }
           if (f.exists()) {
               output.print((char)ch); //add to hold.txt
           }

The files all get created, and I know the serial communication works. But for some reason, I never get the "I found it" message. Am I not using dataPath correctly? Or is it something with exists()? Also, when I create record.txt I do so in the same folder that the code is saved to.

Answers

  • edited April 2015

    Sorry I didn't format it! Now it is legible.

  • The code is not legible. Use this link to format your code.

  • edited April 2015

    Anything that depends on sketch's path, like dataPath() or canvas' dimensions like width & height, should be after setup() starts! :-&

    So instead of File f = new File(dataPath("record.txt")); outside setup()...
    Move the initialization part into setup() after size():

    File f;
    PrintWriter output;
    
    void setup() {
      size(300, 200, JAVA2D);
    
      f = new File(dataPath("record.txt"));
      output = createWriter("hold.txt");
    }
    
  • Thanks for the suggestion, but I've tried that and it still doesn't work. I made an attempt to switch it up a little bit (just because), and am still having no luck. Now the setup for f = new File(dataPath("record.txt")); is in the setup()

    while(!f.exists()) {
       println(while_test); //print integer just to see if i'm still in while loop
       while_test = while_test + 1;
    }
    println("OUTSIDE WHILE LOOP");
    output.print((char)ch); //add to hold.txt
    
  • Show new code.
    Note that the tight loop you show can use 100% CPU... Perhaps add a delay() call inside it.

  • I added a delay, still with no luck though. Here's my current code (minus a few variable definitions and the serial port stuff)...

    PrintWriter output;
    File f;
    
    void setup() {
       size(300, 200, JAVA2D); //I don't need this, only added it bc it was suggested 
       f = new File(dataPath("record.txt"));
       output = createWriter("hold.txt");
     .....
    }
    
    void draw() {
     //filled with some code not relevant to my issue
    }
    
    void serialEvent(Serial port) {
       while(port.available() > 0) {
          int ch = port.read();
          while(!f.exist()) { 
             delay(10); //delay a tenth of a second
             println(while_test);
             while_test = while_test + 1;
          }
          println("OUTSIDE WHILE LOOP");
          output.print((char)ch); //add to hold.txt
    }
    
    void delay(int delay) {
       int time = millis(); //time = # of milliseconds since starting program
       while(mills() - time <= delay);
    }
    
  • Mmm, by delay, I meant a call to delay(), like delay(100); // A tenth of second
    But the reference of this function disappeared... I generally tell to avoid using it, but in some cases like here, it can make sense.

    Your hand-made delay suffers of the same tight loop problem than your original code... It swamps the CPU and doesn't return control to the system (at least, in its thread).

    That said, I don't even understand what you try to do in this loop. Is the file created by another program?

    Note about your createWrite(): without a path, you don't know where the file will end... You should use dataPath() or sketchPath here too.

  • PhiLho, I have a MATLAB program that will be creating "record.txt". As for a call to delay, is that not what I'm doing?

    With createWriter(), I know the path to the file will be wherever the sketch is saved. I can't find anything on that fully describes what dataPath and sketchPath do. Could you shed some light on them?

  • delay() is a built-in function of Processing, no need to define your own.
    Should you supply it yourself, you better call Thread.sleep() (but you need to wrap it in a try / catch structure).

    And yes, you are right, I mixed up createWriter() (Processing function that know about sketch path) and another Java function (that doesn't know about it).

    dataPath / sketchPath: there used to be a JavaDoc for Processing, but I don't know if it is still available. Otherwise, the best information is in the source of the PApplet class...

Sign In or Register to comment.