FileManager
Read and write files on the local file system
FileManager provides access to the local file system and iCloud storage.
Use FileManager to:
Examples
let fm = FileManager.local();
fm.writeString("test.txt", "Hello World");
let content = fm.readString("test.txt");
console.log(content);let fm = FileManager.local();
let files = fm.listContents(fm.documentsDirectory());
files.forEach(f => console.log(f));Methods
read
Read file as base64 data
read(filePath): string | nullfilePath- Path to the file
Returns: Base64 encoded string of file contents
Example:
let data = fm.read("file.bin")readString
Read file as string
readString(filePath): string | nullfilePath- Path to the file
Returns: File contents as UTF-8 string
Example:
let text = fm.readString("file.txt")readImage
Read file as image
readImage(filePath): object | nullfilePath- Path to the image file
Returns: Image data with base64, width, and height
Example:
let img = fm.readImage("photo.png")write
Write data to file
write(filePath, content): voidfilePath- Path to the filecontent- Content to write (Data, string, or base64)
Example:
fm.write("file.txt", data)writeString
Write string to file
writeString(filePath, content): voidfilePath- Path to the filecontent- String content to write
Example:
fm.writeString("file.txt", "content")writeImage
Write image to file
writeImage(filePath, imageData): voidfilePath- Path to the fileimageData- Image data object with base64 property
Example:
fm.writeImage("photo.png", imageData)remove
Delete a file or directory
remove(filePath): voidfilePath- Path to delete
Example:
fm.remove("old-file.txt")move
Move a file or directory
move(sourceFilePath, destinationFilePath): voidsourceFilePath- Source pathdestinationFilePath- Destination path
Example:
fm.move("old.txt", "new.txt")copy
Copy a file or directory
copy(sourceFilePath, destinationFilePath): voidsourceFilePath- Source pathdestinationFilePath- Destination path
Example:
fm.copy("original.txt", "backup.txt")fileExists
Check if file exists
fileExists(filePath): booleanfilePath- Path to check
Returns: true if file exists
Example:
if (fm.fileExists("file.txt")) { ... }isDirectory
Check if path is a directory
isDirectory(path): booleanpath- Path to check
Returns: true if path is a directory
Example:
if (fm.isDirectory("folder")) { ... }createDirectory
Create a directory
createDirectory(path, intermediateDirectories): voidpath- Directory path to createintermediateDirectories- Create intermediate directories if needed
Example:
fm.createDirectory("new/nested/dir", true)temporaryDirectory
Get temporary directory path
temporaryDirectory(): stringReturns: Path to temporary directory
Example:
let tmp = fm.temporaryDirectory()cacheDirectory
Get cache directory path
cacheDirectory(): stringReturns: Path to cache directory
Example:
let cache = fm.cacheDirectory()documentsDirectory
Get documents directory path
documentsDirectory(): stringReturns: Path to documents directory
Example:
let docs = fm.documentsDirectory()libraryDirectory
Get library directory path
libraryDirectory(): stringReturns: Path to library directory
Example:
let lib = fm.libraryDirectory()joinPath
Join two path components
joinPath(lhsPath, rhsPath): stringlhsPath- First path componentrhsPath- Second path component
Returns: Combined path
Example:
let path = fm.joinPath(fm.documentsDirectory(), "file.txt")fileName
Get file name from path
fileName(filePath, includeFileExtension): stringfilePath- Full file pathincludeFileExtension- Include extension in result
Returns: File name
Example:
let name = fm.fileName("/path/to/file.txt", false)fileExtension
Get file extension
fileExtension(filePath): stringfilePath- File path
Returns: File extension including dot (e.g., ".txt")
Example:
let ext = fm.fileExtension("file.txt")creationDate
Get file creation date
creationDate(filePath): Promise | nullfilePath- File path
Returns: JavaScript Date object
Example:
let date = fm.creationDate("file.txt")modificationDate
Get file modification date
modificationDate(filePath): Promise | nullfilePath- File path
Returns: JavaScript Date object
Example:
let date = fm.modificationDate("file.txt")fileSize
Get file size in KB
fileSize(filePath): numberfilePath- File path
Returns: File size in kilobytes
Example:
let size = fm.fileSize("file.txt")allTags
Get all tags for a file
allTags(): string[]addTag
Add a tag to a file
addTag(): voidremoveTag
Remove a tag from a file
removeTag(): voidreadExtendedAttribute
Read extended attribute
readExtendedAttribute(): string | nullwriteExtendedAttribute
Write extended attribute
writeExtendedAttribute(): voidremoveExtendedAttribute
Remove extended attribute
removeExtendedAttribute(): voidallExtendedAttributes
List all extended attributes
allExtendedAttributes(): string[]listContents
List contents of a directory
listContents(directoryPath): string[]directoryPath- Directory path
Returns: Array of file names in the directory
Example:
let files = fm.listContents(fm.documentsDirectory())getUTI
Get Uniform Type Identifier for file
getUTI(filePath): string | nullfilePath- File path
Returns: UTI string
Example:
let uti = fm.getUTI("image.png")downloadFileFromiCloud
Download file from iCloud
downloadFileFromiCloud(filePath, callback): voidfilePath- File pathcallback- Callback when download completes
Example:
await fm.downloadFileFromiCloud("file.txt")isFileStoredIniCloud
Check if file is stored in iCloud
isFileStoredIniCloud(): booleanisFileDownloaded
Check if iCloud file is downloaded locally
isFileDownloaded(): booleanlocal
Get local file manager
static local(): FileManagerReturns: FileManager for local documents directory
Example:
let fm = FileManager.local()iCloud
Get iCloud file manager
static iCloud(): FileManagerReturns: FileManager for iCloud documents
Example:
let fm = FileManager.iCloud()