1# @ohos.file.fileuri (文件URI)
2
3该模块提供通过PATH获取文件统一资源标志符(Uniform Resource Identifier,URI),后续可通过使用[@ohos.file.fs](js-apis-file-fs.md)进行相关open、read、write等操作,实现文件分享。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import { fileUri } from '@kit.CoreFileKit';
13```
14
15使用该功能模块前,需要先获取其应用沙箱路径,开发示例如下:
16
17  ```ts
18  import { UIAbility } from '@kit.AbilityKit';
19  import { window } from '@kit.ArkUI';
20
21  export default class EntryAbility extends UIAbility {
22    onWindowStageCreate(windowStage: window.WindowStage) {
23      let context = this.context;
24      let pathDir = context.filesDir;
25    }
26  }
27  ```
28
29## FileUri<sup>10+</sup>
30
31### 属性
32
33**系统能力**:SystemCapability.FileManagement.AppFileService
34
35| 名称 | 类型 | 必填 | 说明 |
36| -------- | --------| -------- | -------- |
37| path<sup>10+</sup> | string | 是 | 获取FileUri对应路径名。 |
38| name<sup>10+</sup> | string | 是 | 获取FileUri对应文件名。 |
39
40### constructor<sup>10+</sup>
41
42constructor(uriOrPath: string)
43
44constructor是FileUri的构造函数。
45
46**系统能力:** SystemCapability.FileManagement.AppFileService
47
48**参数:**
49
50| 参数名 | 类型 | 必填 | 说明 |
51| -------- | -------- | -------- | -------- |
52| uriOrPath | string | 是 | URI或路径。URI类型:<br/>-&nbsp; 应用沙箱URI:file://\<bundleName>/\<sandboxPath> <br/>-&nbsp; 公共目录文件类URI:file://docs/storage/Users/currentUser/\<publicPath> <br/>-&nbsp; 公共目录媒体类URI:file://media/\<mediaType>/IMG_DATATIME_ID/\<displayName> |
53
54**错误码:**
55
56以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
57| 错误码ID                     | 错误信息        |
58| ---------------------------- | ---------- |
59| 13900005 | I/O error |
60| 13900042 | Unknown error |
61| 13900020 | invalid argument |
62| 13900002 | invalid uri |
63
64**示例:**
65
66  ```ts
67  let path = pathDir + '/test';
68  let uri = fileUri.getUriFromPath(path);  // file://<packageName>/data/storage/el2/base/haps/entry/files/test
69  let fileUriObject = new fileUri.FileUri(uri);
70  console.info("The name of FileUri is " + fileUriObject.name);
71  ```
72
73### toString<sup>10+</sup>
74
75toString(): string
76
77**系统能力:** SystemCapability.FileManagement.AppFileService
78
79返回字符串类型URI。
80
81**返回值:**
82
83| 类型 | 说明 |
84| -------- | -------- |
85| string | 返回字符串类型URI。 |
86
87**示例:**
88
89  ```ts
90  let path = pathDir + '/test';
91  let fileUriObject = new fileUri.FileUri(path);
92  console.info("The uri of FileUri is " + fileUriObject.toString());
93  ```
94
95### getFullDirectoryUri<sup>11+</sup>
96
97getFullDirectoryUri(): string
98
99通过文件或文件夹URI获取当前所在路径的URI。
100
101如果当前FileUri指向文件,将返回文件所在路径URI。如`xxx/example.txt`,将返回`xxx`。
102
103如果当前FileUri指向目录,将返回当前路径URI。
104
105**系统能力**:SystemCapability.FileManagement.AppFileService
106
107**返回值:**
108
109| 类型                  | 说明                                |
110| --------------------- |-----------------------------------|
111| string | 获取所在路径URI,文件获取所在路径URI,目录获取当前路径URI。 |
112
113**错误码:**
114
115以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
116
117| 错误码ID                     | 错误信息                      |
118| ---------------------------- |---------------------------|
119| 13900002 | No such file or directory |
120| 13900012 | Permission denied         |
121| 13900042 | Unknown error             |
122
123**示例:**
124
125  ```ts
126  import { BusinessError } from '@kit.BasicServicesKit';
127  try {
128    let path = pathDir + '/test.txt';
129    let fileUriObject = new fileUri.FileUri(path);
130    let directoryUri = fileUriObject.getFullDirectoryUri();
131    console.log(`success to getFullDirectoryUri: ${JSON.stringify(directoryUri)}`);
132  } catch (error) {
133    console.error(`failed to getFullDirectoryUri because: ${JSON.stringify(error)}`);
134  }
135  ```
136
137### isRemoteUri<sup>12+</sup>
138
139isRemoteUri(): boolean
140
141判断当前URI是否是远端URI。
142
143**系统能力**:SystemCapability.FileManagement.AppFileService
144
145**返回值:**
146
147| 类型                  | 说明                                |
148| --------------------- |-----------------------------------|
149| boolean | - 返回true,表示当前FileUri指向远端文件或目录,如`xxx/example.txt?networkid=xxx`。<br>- 返回false,表示当前FileUri指向本地的文件或目录。 |
150
151**错误码:**
152
153以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
154
155| 错误码ID                     | 错误信息                      |
156| ---------------------------- |---------------------------|
157| 13900042 | Unknown error             |
158
159**示例:**
160
161  ```ts
162  import { BusinessError } from '@kit.BasicServicesKit';
163  function isRemoteUriExample() {
164    let uri = "file://com.example.demo/data/stroage/el2/base/test.txt?networkid=xxxx";//?networkid设备id,远端URI的标识
165    let fileUriObject = new fileUri.FileUri(uri);
166    let ret = fileUriObject.isRemoteUri();
167    if (ret) {
168        console.log(`It is a remote uri.`);
169    }
170  }
171  ```
172
173## fileUri.getUriFromPath
174
175getUriFromPath(path: string): string
176
177以同步方法获取文件URI。
178
179**系统能力**:SystemCapability.FileManagement.AppFileService
180
181**参数:**
182
183| 参数名 | 类型   | 必填 | 说明                       |
184| ------ | ------ | ---- | -------------------------- |
185| path   | string | 是   | 文件的沙箱路径。 |
186
187**返回值:**
188
189  | 类型                           | 说明         |
190  | ---------------------------- | ---------- |
191  | string | 返回文件URI。 |
192
193**错误码:**
194
195以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
196| 错误码ID                     | 错误信息        |
197| ---------------------------- | ---------- |
198| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types |
199
200**示例:**
201
202  ```ts
203  let filePath = pathDir + "/test";
204  let uri = fileUri.getUriFromPath(filePath);
205  ```