openharmony 鸿蒙 js-apis-system-file

2025-06-12 浏览 (1)

@system.file (File Storage)

NOTE

  • The APIs provided by this module are no longer maintained since API Version 10. You are advised to use @ohos.file.fs.
  • 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 file to the given location.

NOTE

This API is deprecated since API version 10. Use fs.moveFile instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
srcUristringYesUniform resource identifier (URI) of the file to move.
The URI can contain a maximum of 128 characters, excluding the following characters: "*+,:;<=>?[]|\x7F
dstUristringYesURI of the location to which the file is to move.
The URI can contain a maximum of 128 characters, excluding the following characters: "*+,:;<=>?[]|\x7F
successFunctionNoCalled when the file is moved to the specified location. This API returns the URI of the destination location.
failFunctionNoCalled when the file fails to be moved.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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 to the given URI.

NOTE

This API is deprecated since API version 10. Use fs.copyFile instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
srcUristringYesURI of the file to copy.
dstUristringYesURI of the location to which the copy is to be saved.
The directory of application resources and URI of the tmp type are not supported.
successFunctionNoCalled when the file is copied and saved to the specified location. This API returns the URI of the destination location.
failFunctionNoCalled when the file fails to be copied.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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 all files in the specified directory.

NOTE

This API is deprecated since API version 10. Use fs.listFile instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of the directory.
The URI can contain a maximum of 128 characters, excluding the following characters: "*+,:;<=>?[]|\x7F
successFunctionNoCalled when the file list is obtained.
failFunctionNoCalled when the file list fails to be obtained.
completeFunctionNoCalled when the execution is complete.

Return value of success()

NameTypeDescription
fileListArray<FileInfo>File list. The format of each file is as follows:
{
uri:'file1',
lastModifiedTime:1589965924479,
length:10240,
type: 'file'
}

Table 1 FileInfo

NameTypeDescription
uristringURI of the file.
lastModifiedTimenumberTimestamp when the file is saved the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.
lengthnumberFile size, in bytes.
typestringFile type. Available values are as follows:
- dir: directory
file: file

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does not exist.

Example

export default {    
  list() {        
    file.list({            
      uri: 'internal://app/pic',            
      success: function(data) {                
        console.log(JSON.stringify(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 local file.

NOTE

This API is deprecated since API version 10. Use fs.stat instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of the file.
recursivebooleanNoWhether to obtain the subdirectory file list recursively. The value true means to obtain the subdirectory file list recursively; the value false means the opposite.
successFunctionNoCalled when the file information is obtained.
failFunctionNoCalled when the file information fails to be obtained.
completeFunctionNoCalled when the execution is complete.

Return value of success()

NameTypeDescription
uristringURI of the file.
lengthnumberFile size, in bytes.
lastModifiedTimenumberTimestamp when the file is saved the last time, which is the number of milliseconds elapsed since 1970/01/01 00:00:00 GMT.
typestringFile type. Available values are as follows:
-  dir: directory
file: file
subFilesArrayList of files.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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 a local file.

NOTE

This API is deprecated since API version 10. Use fs.unlink instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of the file to delete. It cannot be an application resource path.
successFunctionNoCalled when the file is deleted.
failFunctionNoCalled when the file fails to be deleted.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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 file. Only text files can be read and written.

NOTE

This API is deprecated since API version 10. Use fs.write instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of a local file. If it does not exist, a file will be created.
textstringYesText to write into the file.
encodingstringNoEncoding format. The default format is UTF-8.
appendbooleanNoWhether to enable the append mode. The default value is false. The value true means to enable the append mode; the value false means the opposite.
successFunctionNoCalled when the text is written into the specified file.
failFunctionNoCalled when the text fails to be written into the file.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.

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 file. Only text files can be read and written.

NOTE

This API is deprecated since API version 10. Use fs.write instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of a local file. If it does not exist, a file will be created.
bufferUint8ArrayYesBuffer from which the data is derived.
positionnumberNoOffset to the position where the writing starts. The default value is 0.
appendbooleanNoWhether to enable the append mode. The default value is false. If the value is true, the position parameter will become invalid. The value true means to enable the append mode; the value false means the opposite.
successFunctionNoCalled when buffer data is written into the file.
failFunctionNoCalled when buffer data fails to be written into the file.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.

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 file. Only text files can be read and written.

NOTE

This API is deprecated since API version 10. Use fs.readText instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of a local file.
encodingstringNoEncoding format. The default format is UTF-8.
positionnumberNoPosition where the reading starts. The default value is the start position of the file.
lengthnumberNoLength of the text to read, in bytes. The default value is 4096.
successFunctionNoCalled when the text is read successfully.
failFunctionNoCalled when the text failed to be read.
completeFunctionNoCalled when the execution is complete.

Return value of success()

NameTypeDescription
textstringText read from the specified file.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does not exist.
302The text to read exceeds 4 KB.

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 file. Only text files can be read and written.

NOTE

This API is deprecated since API version 10. Use fs.read instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of a local file.
positionnumberNoPosition where the reading starts. The default value is the start position of the file.
lengthnumberNoLength of data to read. If this parameter is not set, the reading proceeds until the end of the file.
successFunctionNoCalled when the buffer data is read successfully.
failFunctionNoCalled when the buffer data fails to be read.
completeFunctionNoCalled when the execution is complete.

Return value of success()

NameTypeDescription
bufferUint8ArrayData read.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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 file or directory exists.

NOTE

This API is deprecated since API version 10. Use fs.access instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of the directory or file to check.
successFunctionNoCalled when the operation is successful.
failFunctionNoCalled when the operation fails.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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 directory.

NOTE

This API is deprecated since API version 10. Use fs.mkdir instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of the directory to create.
recursivebooleanNoWhether to recursively create upper-level directories of the specified directory. The default value is false.
successFunctionNoCalled when the directory is created.
failFunctionNoCalled when the directory fails to be created.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.

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 directory.

NOTE

This API is deprecated since API version 10. Use fs.rmdir instead.

System Capability: SystemCapability.FileManagement.File.FileIO.Lite

Parameters

NameTypeMandatoryDescription
uristringYesURI of the directory to delete.
recursivebooleanNoWhether to recursively delete files and subdirectories of the specified directory. The default value is false. The value true means to recursively delete files and subdirectories of the specified directory; the value false means the opposite.
successFunctionNoCalled when the directory is deleted.
failFunctionNoCalled when the directory fails to be deleted.
completeFunctionNoCalled when the execution is complete.

Error codes

Error CodeDescription
202Incorrect parameters are detected.
300An I/O error occurs.
301The file or directory does 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);            
      },
    });    
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/jfXt80