File Storage
Note:
The APIs of this module are no longer maintained since API version 6. It is recommended that you use
@ohos.fileio
instead.The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
Modules to Import
import file from '@system.file';
file.move
move(Object): void
Moves a specified file to a given location.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
srcUri | string | Yes | URI of the file to move. |
dstUri | string | Yes | URI of the location to which the file is to move. |
success | Function | No | Called when the source file is moved to the specified location successfully. This function returns the URI of the destination location. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
move() {
file.move({
srcUri: 'internal://app/myfiles1',
dstUri: 'internal://app/myfiles2',
success: function(uri) {
console.log('call success callback success');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.copy
copy(Object): void
Copies a file and saves the copy to a specified location.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
srcUri | string | Yes | URI of the file to copy. |
dstUri | string | Yes | URI of the location to which the copy is to save. The directory of application resources and URI of the tmp type are not supported. |
success | Function | No | Called when the source file is copied and saved to the specified location successfully. This function returns the URI of the destination location. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
copy() {
file.copy({
srcUri: 'internal://app/file.txt',
dstUri: 'internal://app/file_copy.txt',
success: function(uri) {
console.log('call success callback success');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.list
list(Object): void
Obtains the list of all files in a specified directory.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of the directory. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
Return values of the success callback
Name | Type | Description |
---|---|---|
fileList | Array<FileInfo> | File list. The format of each file is as follows: { uri:'file1', lastModifiedTime:1589965924479, length:10240, type: 'file' } |
Table1 FileInfo
Name | Type | Description |
---|---|---|
uri | string | File URI. |
lastModifiedTime | number | Timestamp when the file is stored the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT. |
length | number | File size, in bytes. |
type | string | File type. Available values are as follows: - dir: directory - file: file |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
list() {
file.list({
uri: 'internal://app/pic',
success: function(data) {
console.log(data.fileList);
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.get
get(Object): void
Obtains information about a specified local file.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | File URI. |
recursive | boolean | No | Whether to recursively obtain the file list under a subdirectory. The default value is false. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
Return values of the success callback
Name | Type | Description |
---|---|---|
uri | string | File URI. |
length | number | File size, in bytes. |
lastModifiedTime | number | Timestamp when the file is stored the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT. |
type | string | File type. The values are as follows: - dir: directory - file: file |
subFiles | Array | File list. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
get() {
file.get({
uri: 'internal://app/file',
success: function(data) {
console.log(data.uri);
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.delete
delete(Object): void
Deletes local files.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of the file to delete, which cannot be an application resource path. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Incorrect parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
delete() {
file.delete({
uri: 'internal://app/my_file',
success: function() {
console.log('call delete success.');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.writeText
writeText(Object): void
Writes text into a specified file.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of a local file. If it does not exist, a file will be created. |
text | string | Yes | Character string to write into the local file. |
encoding | string | No | Encoding format. The default format is UTF-8. |
append | boolean | No | Whether to enable the append mode. The default value is false. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Incorrect parameter. |
300 | I/O error. |
Example
export default {
writeText() {
file.writeText({
uri: 'internal://app/test.txt',
text: 'Text that just for test.',
success: function() {
console.log('call writeText success.');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.writeArrayBuffer
writeArrayBuffer(Object): void
Writes buffer data into a specified file.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of a local file. If it does not exist, a file will be created. |
buffer | Uint8Array | Yes | Buffer from which the data is derived. |
position | number | No | Offset to the position where the writing starts. The default value is 0. |
append | boolean | No | Whether to enable the append mode. The default value is false. If the value is true, the position parameter will become invalid. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
Example
export default {
writeArrayBuffer() {
file.writeArrayBuffer({
uri: 'internal://app/test',
buffer: new Uint8Array(8), // The buffer is of the Uint8Array type.
success: function() {
console.log('call writeArrayBuffer success.');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.readText
readText(Object): void
Reads text from a specified file.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of a local file. |
encoding | string | No | Encoding format. The default format is UTF-8. |
position | number | No | Position where the reading starts. The default value is the start position of the file. |
length | number | No | Length of the text to be read (in bytes). The default value is 4096. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
Return values of the success callback
Name | Type | Description |
---|---|---|
text | string | Text read from the specified file. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | The file or directory does not exist. |
302 | The size of text to read exceeds 4096 bytes. |
Example
export default {
readText() {
file.readText({
uri: 'internal://app/text.txt',
success: function(data) {
console.log('call readText success: ' + data.text);
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.readArrayBuffer
readArrayBuffer(Object): void
Reads buffer data from a specified file.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of a local file. |
position | number | No | Position where the reading starts. The default value is the start position of the file. |
length | number | No | Length of data to read. If this parameter is not set, the reading proceeds until the end of the file. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
Return values of the success callback
Name | Type | Description |
---|---|---|
buffer | Uint8Array | File content that is read |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
readArrayBuffer() {
file.readArrayBuffer({
uri: 'internal://app/test',
position: 10,
length: 200,
success: function(data) {
console.log('call readArrayBuffer success: ' + data.buffer);
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.access
access(Object): void
Checks whether a specified file or directory exists.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of the directory or file. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
access() {
file.access({
uri: 'internal://app/test',
success: function() {
console.log('call access success.');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.mkdir
mkdir(Object): void
Creates a specified directory.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of the directory. |
recursive | boolean | No | Whether to recursively create upper-level directories of the specified directory. The default value is false. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
Example
export default {
mkdir() {
file.mkdir({
uri: 'internal://app/test_directory',
success: function() {
console.log('call mkdir success.');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}
file.rmdir
rmdir(Object): void
Deletes a specified directory.
System capability: SystemCapability.FileManagement.File.FileIO
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
uri | string | Yes | URI of the directory. |
recursive | boolean | No | Whether to recursively delete subfiles and subdirectories of the specified directory. The default value is false. |
success | Function | No | Called when the operation is successful. |
fail | Function | No | Called when the operation fails. |
complete | Function | No | Called when the execution is complete. |
One of the following error codes will be returned if the operation fails.
Error Code | Description |
---|---|
202 | Invalid parameter. |
300 | I/O error. |
301 | File or directory not exist. |
Example
export default {
rmdir() {
file.rmdir({
uri: 'internal://app/test_directory',
success: function() {
console.log('call rmdir success.');
},
fail: function(data, code) {
console.error('call fail callback fail, code: ' + code + ', data: ' + data);
},
});
}
}