isDirectory()
描述
判断 File 物件所开启的是否是资料夹。
语法
file.isDirectory()
参数
file:被 SD.open() 回传的 File 物件
回传
bool:如果 File 物件所开启的是资料夹则回传 true,反之则回传 false
范例
#include <SD.h>
File root;
void setup()
{
Serial.begin(9600);
SD.begin();
root = SD.open("/");
printDirectory(root, 0);
Serial.println("done!");
}
void loop()
{
// 不做任何事
}
void printDirectory(File dir, int numTabs) {
while(true) {
File entry = dir.openNextFile();
if (! entry) {
// 没有未被开启的档案了
// Serial.println("**nomorefiles**");
break;
}
for (uint8_t i=0; i<numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs+1);
} else {
// 如果是档案会印出档案大小
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
See Also
- openNextFile()
- rewindDirectory()
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.
