recreate() use this function as alternative to create(). If the file with this name does not exist, it will be created. If it does exist, then the function will delete the content and creates the file again. The advantage is that recreate() tries to reuse the previously occupied memory space (if the new content size fits in).
Hint: Simulate writing ( getWriteCounter() ) to determine the actually required size of the file before actually creating it.


recreate( filename, length ); – creates or recreates a ‘normal’ file.
recreate( filename, length, nb_of_records ); – creates or recreates a ‘record set’ file consisting of nb_of_records x length p. record.
recreate( filename, length, nb_of_records, file_version ); – creates or recreates a ‘record set’ file and gives it a version number


char* filename – pointer a zero-terminated char array holding the file name
uint32_t length – size of the file (normal) or the size of each ‘record’ (for record-set file)
uint32_t nb_of_records – number of records each of length bytes. (this parameter >0 makes it a RecSet type file)
uint32_t file_version – a version number (or 0) of the file


– true if the file could be created
– false if not

Example Code

#include "SerialFlash.h"
void setup()
  if( SerialFlash.recreate("myFile.txt", 1024) == false )
    Serial.println("Normal file could NOT be created");

  if( SerialFlash.recreate("myFileRecSet.txt", 16, 1000, 123) == false )
    Serial.println("RecSet file could NOT be created");
void loop () 
{ }

see example ‘File -> Examples -> SerialFlash -> ListFileDetails’ for the result of the example code above

Notes and Warnings

By applying a third parameter (nb_of_records > 0) a file of type record-set will be created (instead of a normal file). This type can be helpful if the actual size of the file matters, e.g. for binary data. It might also be important when it comes to interpreting or transferring (e.g. via CAN2SER, internet) files. In cases like this, simply use nb_of_records = 1.
Another use case for recordset files are for logging data in predictable chunks e.g. logging CAN messages or GPS coordinates etc. Recordset files provide an automatic bad-block management. Corrupted blocks (records) due to sudden lost of power are automatically marked ‘bad’ and will be ignored when the file is read or continued.
Simulated writing: Find examples in the Arduino IDE example menu:
SerialFlash-> SimulateBeforeWriteFile

See also

GitHub – PaulStoffregen/SerialFlash – org. code this library is based on
c_str() – function to create a 0-terminated char array from a String
erase() – delete content of a file only
remove() – remove from directory
open() – open a file for read/write operations
create() – ‘standard’ file create method