e6b113941140350e2e01e37bfbd9695591c22d5f
[platform/upstream/iotjs.git] / docs / api / IoT.js-API-File-System.md
1 ## Module: fs
2
3 ### Platform Support
4
5 The following shows fs module APIs available for each platform.
6
7 |  | Linux<br/>(Ubuntu) | Raspbian<br/>(Raspberry Pi) | Nuttx<br/>(STM32F4-Discovery) |
8 | :---: | :---: | :---: | :---: |
9 | fs.close | O | O | O |
10 | fs.closeSync | O | O | O |
11 | fs.open | O | O | O |
12 | fs.openSync | O | O | O |
13 | fs.read | O | O | O |
14 | fs.readSync | O | O | O |
15 | fs.readDir | O | O | X |
16 | fs.readDirSync | O | O | X |
17 | fs.readFile | O | O | O |
18 | fs.readFileSync | O | O | O |
19 | fs.rename | O | O | O |
20 | fs.renameSync | O | O | O |
21 | fs.stat | O | O | O |
22 | fs.statSync | O | O | O |
23 | fs.fstat | O | O | O |
24 | fs.fstatSync | O | O | O |
25 | fs.write | O | O | O |
26 | fs.writeSync | O | O | O |
27 | fs.writeFile | O | O | O |
28 | fs.writeFileSync | O | O | O |
29 | fs.unlink | O | O | O |
30 | fs.unlinkSync | O | O | O |
31
32 ※ On `nuttx` path should be passed with a form of **absolute path**.
33
34
35 ### Methods
36
37 #### fs.close(fd, callback)
38
39 * `fd: Int` - file descriptor.
40 * `callback: Function(err) `
41  * `err: Error`
42
43 Closes the file of fd asynchronously.
44
45
46 #### fs.closeSync(fd)
47
48 * `fd: Int` - file descriptor.
49
50 Closes the file of fd synchronously.
51
52
53 #### fs.open(path, flags[, mode], callback)
54
55 * `path: String` - file path to be opened.
56 * `flags: String` - open flags.
57 * `mode: Number`, Default: `0666` - permission mode.
58 * `callback: Function(err, fd)`
59  * `err: Error` - `Error` object if there was something wrong, otherwise `null`.
60  * `fd: Number` - file descriptor.
61
62
63 #### fs.openSync(path, flags[, mode])
64
65 * `path: String` - file path to be opened.
66 * `flags: String` - open flags.
67 * `mode: Number`, Default: `0666` - permission mode.
68
69
70 #### fs.read(fd, buffer, offset, length, position, callback)
71
72 * `fd: Int` - file descriptor.
73 * `buffer: Buffer` - buffer that the data will be written to.
74 * `offset: Number` - offset of the buffer where to start writing.
75 * `length: Number` - number of bytes to read.
76 * `position: Number` - specifying where to start read data from the file, if `null`, read from current position.
77 * `callback: Function(err, bytesRead, buffer) `
78  * `err: Error`
79  * `bytesRead: Number`
80  * `buffer: Buffer`
81
82
83 #### fs.readSync(fd, buffer, offset, length, position)
84
85 * `fd: Int` - file descriptor.
86 * `buffer: Buffer` - buffer that the data will be written to.
87 * `offset: Number` - offset of the buffer where to start writing.
88 * `length: Number` - number of bytes to read.
89 * `position: Number` - specifying where to start read data from the file, if `null`, read from current position.
90
91
92 #### fs.readFile(path[, options], callback)
93
94 * `path: String` - file path to be opened.
95 * `options: Object` - options for the operation.
96  * `encoding: String`, Default: `null` - encoding of the file.
97  * `flag: String`, Default: `r` - file open flag.
98 * `callback: Function(err, data)` - callback function.
99  * `err: Error`
100  * `data: Buffer`
101
102 Reads entire file asynchronously.
103
104
105 #### fs.readFileSync(path[, options])
106
107 * `path: String` - file path to be opened.
108 * `options: Object` - options for the operation.
109  * `encoding: String`, Default: `null` - encoding of the file.
110  * `flag: String`, Default: `r` - file open flag.
111
112 Reads entire file synchronously.
113
114
115 #### fs.rename(oldPath, newPath, callback)
116
117 * `oldPath: String` - old file path
118 * `newPath: String` - new file path
119 * `callback: Function(err)` - callback function.
120  * `err: Error`
121
122 Renames `oldPath` to `newPath` asynchronously.
123
124
125 #### fs.renameSync(oldPath, newPath)
126
127 * `oldPath: String` - old file path
128 * `newPath: String` - new file path
129
130 Renames `oldPath` to `newPath` synchronously.
131
132
133 #### fs.stat(path, callback)
134
135 * `path: String` - file path to be stated
136 * callback: Function(err, stat)` - callback function.
137  * `err: Error`
138  * `stat: Object`
139
140
141 #### fs.statSync(path)
142
143 * `path: String` - file path to be stated
144
145
146 #### fs.fstat(fd, callback)
147
148 * `fd: Number` - file descriptor to be stated
149 * callback: Function(err, stat)` - callback function.
150  * `err: Error`
151  * `stat: Object`
152
153
154 #### fs.fstatSync(fd)
155
156 * `fd: Number` - file descriptor to be stated
157
158  ### Class: fs.Stats
159
160  fs.Stats class is a object returned from ```fs.stat()```,```fs.fstat()``` and their synchronous counterparts.
161
162  ### Methods
163
164  #### stats.isDirectory()
165
166  Returns true if stated file is a directory
167
168  #### stats.isFile()
169
170  Returns true if stated file is a file
171
172
173 #### fs.write(fd, buffer, offset, length, position, callback)
174
175 * `fd: Int` - file descriptor.
176 * `buffer: Buffer` - buffer that the data will be written from.
177 * `offset: Number` - offset of the buffer where from start reading.
178 * `length: Number` - number of bytes to write.
179 * `position: Number` - specifying where to start write data to the file, if `null`, read from current position.
180 * callback: Function(err, bytesWrite)` - callback function.
181  * `err: Error`
182  * `byteWrite: Int`
183
184
185 #### fs.writeSync(fd, buffer, offset, length, position)
186
187 * `fd: Int` - file descriptor.
188 * `buffer: Buffer` - buffer that the data will be written from.
189 * `offset: Number` - offset of the buffer where from start reading.
190 * `length: Number` - number of bytes to write.
191 * `position: Number` - specifying where to start write data to the file, if `null`, read from current position.
192
193
194 #### fs.writeFile(path, data, [, options], callback)
195
196 * `path: String` - file path that the `data` will be written
197 * `data: Buffer` - buffer that contains data
198 * `options: Object` - options for the operation
199 * `callback: Function(err)` - callback function
200  * `err: Error`
201
202 Writes entire `data` to the file specified by `path` asynchronously.
203
204
205 #### fs.writeFileSync(path, data, [, options])
206
207 * `path: String` - file path that the `data` will be written
208 * `data: Buffer` - buffer that contains data
209 * `options: Object` - options for the operation
210
211 Writes entire `data` to the file specified by `path` synchronously.
212
213
214 #### fs.unlink(path, callback)
215
216 * `path: String` - file path to be removed
217 * `callback: Function(err) `
218  * `err: Error`
219
220 Removes the file specified by `path` asynchronously.
221
222
223 #### fs.unlinkSync(path)
224
225 * `path: String` - file path to be removed
226
227 Removes the file specified by `path` synchronously.