how to prevent file overwriting?

edited December 2017 in Programming Questions

Hi, another small issue again. Each time the sensor reads a value, it stores it in a txt file but the file is rewritten in every cycle, so at the end what i get is the last sensor info and not the entire summary.. Some helps.. Please.. (i am new to this whole programming world by the way.. :-D)

Answers

  • Might try dumping the sensor values in an StringList, then have the StringList write to a file.

  • The sensor values are read in a loop, how do you suggest i dump it to a stringlist. Wouldn't it get reset once when the loop starts again?

  • edited December 2017 Answer ✓

    How about saving your logs in separate files named w/ current time stamp? :D

    // Forum.Processing.org/two/discussion/10067/how-to-prevent-file-overwriting#Item_3
    // Docs.Oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
    
    static final String PATTERN = "yyyy-MMM-dd.HH_mm_ss", EXT = ".log";
    String timeStamp, logSavePath;
    
    void setup() {
      timeStamp = getTimeStamp(PATTERN);
      logSavePath = dataPath(timeStamp) + EXT;
    
      println(timeStamp);
      println(logSavePath);
    
      exit();
    }
    
    static final String getTimeStamp(final String pattern) {
      return new java.text.SimpleDateFormat(pattern).format(new java.util.Date());
    }
    
  • edited April 2015

    GoToloop, sorry for the delay.. But i couldn't understand much from that code. Which variable holds the sensor data? Like the serial rfid reader data? (please, do forgive me for my lack of knowledge.. I am just learning you know.. :-D

  • edited April 2015

    It simply generates a stamp name based on current time & date to be used to save your log files.
    Just a silly idea to avoid overwriting the same filename! O:-)

  • Answer ✓

    As lifelike said, you can read the previous file in an ArrayList, append the new values, then save the content of the list. That's the simplest way.
    GoToLoop' solution can work, but you will end with lot of files!

  • Yup.. I saw that.. But i can't actually implement such a code with my present understanding of processing.. I think i will go along with GoToloop's method..

Sign In or Register to comment.