Skip to content

kylegoetz/react-native-filesystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

react-native-filesystem

Simple file system API for iOS & Android, for dealing with text-files.

All interaction is promise-based, and all content is written and read as UTF-8.

This library is still under development, and only works on iOS at the current moment.

Setup

npm install react-native-filesystem --save
react-native link react-native-filesystem

Usage

Write to files

import FileSystem from 'react-native-filesystem';

async function writeFile() {
  const fileContents = 'This is a my content.';
  await FileSystem.writeToFile('my-directory/my-file.txt', fileContents);
  console.log('file is written');
}

Sub-directories are created automatically.

Read from files

async function readFile() {
  const fileContents = await FileSystem.readFile('my-directory/my-file.txt');
  console.log(`read from file: ${fileContents}`);
}

Delete files or folders

async function deleteFile() {
  await FileSystem.delete('my-directory/my-file.txt');
  console.log('file is deleted');
}

Check if files or directories exist

async function checkIfFileExists() {
  const fileExists = await FileSystem.fileExists('my-directory/my-file.txt');
  const directoryExists = await FileSystem.fileExists('my-directory/my-file.txt');
  console.log(`file exists: ${fileExists}`);
  console.log(`directory exists: ${directoryExists}`);
}

Selecting storage class

All commands also take an optional last argument specifying a storage class. These classes roughly correspond to the four points of the iOS Data Storage Guidelines, and have similar behaviour on Android. Example usage:

FileSystem.writeToFile('my-file.txt', 'My content', FileSystem.storage.important);

Files need to be read from the same storage class they're saved to, and two files can have the same name if they're located in different storages. The options are:

storage.backedUp

The default. Files stored in this location will automatically be backed up by iCloud on iOS and Auto Backup for Apps on Android. This is generally for user-generated content that cannot be re-generated / re-downloaded.

Corresponds to <Application_Home>/Documents on iOS and Context.getFilesDir() on Android.

storage.important

This is for files that are possible to re-generate / re-download, but are still important to keep around. F.ex. offline maps.

Corresponds to <Application_Home>/<Application_Home>/Library/Caches with "do not backup" flag on iOS and Context.getNoBackupFilesDir() on Android.

storage.auxiliary

This storage class is for files that can be re-created, and are not crucial to the proper functioning of your app.

Corresponds to <Application_Home>/<Application_Home>/Library/Caches on iOS and Context.getExternalCacheDir() on Android.

storage.temporary

Location for temporary caches and data. You should still clean up / delete the files when they are no longer in use.

Corresponds to <Application_Home>/tmp on iOS and Context.getCacheDir() on Android.

Questions?

Why yet another file system library?

I simply couldn't find one that satisfied my basic needs for simplicity.

Why not use the built-in AsyncStorage?

AsyncStorage is fine, but some times you want more control as to where the content is stored. This library lets you put it in backed-up folders, or play nice by marking content that can be deleted when the phone runs low on space.

About

Simple file system API for iOS & Android.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 42.3%
  • Objective-C 29.7%
  • JavaScript 24.7%
  • Python 3.3%