/*
* Basic SPIFFS Example
*
https://arduino-esp8266.readthedocs.io/en/latest/filesystem.html
*/
#include "FS.h"
void listDir() {
char cwdName[2];
strcpy(cwdName,"/");
Dir dir=SPIFFS.openDir(cwdName);
while( dir.next()) {
String fn, fs;
fn = dir.fileName();
fn.remove(0, 1);
fs = String(dir.fileSize());
Serial.println("<" + fn + ">
size=" + fs);
} // end while
}
void setup() {
delay(500);
Serial.begin(9600);
Serial.println();
Serial.println("\nVery basic Spiffs example,
writing 10 lines to SPIFFS filesystem, and then read them
back");
SPIFFS.begin();
// Next lines have to be done ONLY ONCE!!!!!When
SPIFFS is formatted ONCE you can comment these lines out!!
Serial.println("Please wait 30 secs for SPIFFS to
be formatted");
SPIFFS.format();
Serial.println("Spiffs formatted");
// open file for writing
File fw = SPIFFS.open("/f.txt", "w");
if (!fw) {
Serial.println("file open failed");
} else {
Serial.println("====== Writing to
SPIFFS file =========");
for (int i=1; i<=10; i++){
fw.print("Line : ");
fw.println(i);
} // end for
fw.close();
} // end if
// list directory
listDir();
// open file for reading
File fr = SPIFFS.open("/f.txt", "r");
if (!fr) {
Serial.println("file open failed");
} else {
Serial.println("====== Reading from
SPIFFS file =======");
for (int i=1; i<=10; i++){
String
s=fr.readStringUntil('\n');
Serial.print(i);
Serial.print(":");
Serial.println(s);
} // end for
fr.close();
} // end if
}
void loop() {
}
|