Coder Social home page Coder Social logo

ptzagk / react-native-filesystem Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paandahl/react-native-filesystem

0.0 1.0 0.0 147 KB

Simple file system API for iOS & Android.

License: Apache License 2.0

Java 42.41% Objective-C 29.65% JavaScript 24.61% Python 3.34%

react-native-filesystem's Introduction

react-native-filesystem npm version

Simple file system access on iOS & Android.

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

Setup

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

This project is based on the 9-project-layout.

Usage

For a full list of available methods, see the API Reference.

Write to files

import FileSystem from 'react-native-filesystem';

async function writeToFile() {
  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.directoryExists('my-directory/my-file.txt');
  console.log(`file exists: ${fileExists}`);
  console.log(`directory exists: ${directoryExists}`);
}

Selecting a 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 class Description
storage.backedUp These files are automatically backed up on supported devices
storage.important Excluded from backup, but still kept around in low-storage situations
storage.auxiliary Files that the app can function without. Can be deleted by the system in low-storage situations.
storage.temporary For temporary files and caches. Can be deleted by the system any time.

For full details, see the API Reference.

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.

react-native-filesystem's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.