Sometimes it would be useful to have a function to save data to a text file. 
I plan to save some of my Arduino sensor data to a text file using the 
Processing language.
Ninety percent of the following code comes from this
 forum posting. Thank goodness for the internet, and for people like PhiLho and Cedric. I modified their code and ended up with this (to suit my own project):
 
                       1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
   |  /* =====================================================================      Project : Append data to a text file       Author : ScottC      Created : 16 Nov 2011  (Updated 26 Aug 2014)  Description : Use the Processing language to store data on                the computer. This code can be used to create a                new text file or append to an existing file.                 Processing Version tested : 2.2.1 Reference: http://processing.org/discourse/yabb2/YaBB.pl?num=1251025598/4
  =============================================================================== */
  import java.io.BufferedWriter; import java.io.FileWriter;
  void setup(){    String myFileName = "C:/SensorData/mySensorData.txt";    String mySensorData = "10,20,30,40,50";        //To insert data into a new file    //saveData(myFileName, mySensorData, false);        //To append data to an existing file    saveData(myFileName, mySensorData, true); }
  void draw(){ }
  /* --------------------------------------------------------------   SaveData:   - This code is used to create/append data to a designated file.   - fileName = the location of the output data file (String)     - newData = the data to be stored in the file (String)   - appendData = true if you want to append                = false if you want to create a new file   --------------------------------------------------------------*/ void saveData(String fileName, String newData, boolean appendData){   BufferedWriter bw = null;   try {       FileWriter fw = new FileWriter(fileName, appendData);     bw = new BufferedWriter(fw);     bw.write(newData + System.getProperty("line.separator"));   } catch (IOException e) {   } finally {     if (bw != null){       try {          bw.close();        } catch (IOException e) {}       }   } }
 
  | 
        
  This will append the sensor data of  10,20,30,40,50 to the end of mySensorData.txt file. 
   If I wanted to create a new text file instead, then I would call saveData in the following way: 
   saveData(myFileName, mySensorData, false);
 
If you found this code useful, please support me here: 
  
Description: 
How to append text to a txt file using Processing
Rating: 
3.5
Reviewer: 
Unknown
ItemReviewed: 
How to append text to a txt file using Processing