Skip to content

Commit e7648cc

Browse files
committed
Updated readme
1 parent 147f199 commit e7648cc

File tree

1 file changed

+71
-21
lines changed

1 file changed

+71
-21
lines changed

README.md

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,65 @@ var RNFS = require('react-native-fs');
3030

3131
// get a list of files and directories in the main bundle
3232
RNFS.readDir('/', RNFS.MainBundle)
33-
.then((result) => {
34-
console.log('GOT RESULT', result);
35-
36-
// stat the first file
37-
return Promise.all([RNFS.stat(result[0].path), result[0].path]);
38-
})
39-
.then((statResult) => {
40-
if (statResult[0].isFile()) {
41-
// if we have a file, read it
42-
return RNFS.readFile(statResult[1]);
43-
}
44-
45-
return 'no file';
46-
})
47-
.then((contents) => {
48-
// log the file contents
49-
console.log(contents);
50-
})
51-
.catch((err) => {
52-
console.log(err.message, err.code);
53-
});
33+
.then((result) => {
34+
console.log('GOT RESULT', result);
35+
36+
// stat the first file
37+
return Promise.all([RNFS.stat(result[0].path), result[0].path]);
38+
})
39+
.then((statResult) => {
40+
if (statResult[0].isFile()) {
41+
// if we have a file, read it
42+
return RNFS.readFile(statResult[1]);
43+
}
44+
45+
return 'no file';
46+
})
47+
.then((contents) => {
48+
// log the file contents
49+
console.log(contents);
50+
})
51+
.catch((err) => {
52+
console.log(err.message, err.code);
53+
});
54+
```
55+
56+
### File creation
57+
58+
```javascript
59+
// require the module
60+
var RNFS = require('react-native-fs');
61+
62+
// create a path you want to write to
63+
var path = RNFS.DocumentDirectoryPath + '/test.txt';
64+
65+
// write the file
66+
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet')
67+
.then((success) => {
68+
console.log('FILE WRITTEN!');
69+
})
70+
.catch((err) => {
71+
console.log(err.message);
72+
});
73+
74+
```
75+
76+
### File deletion
77+
```javascript
78+
// create a path you want to delete
79+
var path = RNFS.DocumentDirectoryPath + '/test.txt';
80+
81+
return RNFS.unlink(path)
82+
// spread is a method offered by bluebird to allow for more than a
83+
// single return value of a promise. If you use `then`, you will receive
84+
// the values inside of an array
85+
.spread((success, path) => {
86+
console.log('FILE DELETED', success, path);
87+
})
88+
// `unlink` will throw an error, if the item to unlink does not exist
89+
.catch((err) => {
90+
console.log(err.message);
91+
});
5492
```
5593

5694
## API
@@ -92,3 +130,15 @@ The promise resolves with an object with the following properties:
92130
Reads the file at `path` and - by default - decodes the transferred base64 string. If `shouldDecode` is `false`, the base64 encoded string is returned
93131

94132
Note: you will take quite a performance hit if you are reading big files
133+
134+
### `promise writeFile(filepath, contents [, options])`
135+
136+
Write the `contents` to `filepath`. `options` optionally takes an object specifying the file's properties, like mode etc.
137+
138+
The promise resolves with a boolean.
139+
140+
### `promise unlink(filepath)`
141+
142+
Unlinks the item at `filepath`. If the item does not exists, an error will be thrown.
143+
144+
The promise resolves with an array, which contains a boolean and the path that has been unlinked. Tip: use `spread` to receive the two arguments instead of a single array in your handler.

0 commit comments

Comments
 (0)