鸿蒙存储系统文件系统,鸿蒙liteWearable操作文件系统的正确姿势

10月20号我在官方论坛提了一个问题,请问文件存储接口的根路径是什么?

34e4d9b66faa0f4bae09a6a0d2add1a8.png

原因是在看官方文档学习时测试文件存储的API

6ba542f10536610d68937847a24a8ca6.png

作为一个有经验(一点点)的开发人员,把代码贴到onShow()中直接执行。最终报错301,文件不存在。报错了那一定是我没仔细看文档,再找。

d453a8d321e4c48f1177a3ca61e11000.png

所以文件的前缀是internal://app/那如果我只填这个应该没问题,虽然不知道app对应的根目录是哪里,但现在里边没有我自己放的文件调用下不至于301吧。结果还是301,这就让我迷惑了。于是头铁的认为这是BUG,肯定是BUG。

一周后官方人员给了回复。

29d7af4466f7a7f6ba8eab8f0c85090f.png

f8cd5f7ee8f0d09550f388a814bfd805.png

7c3a334e0bfb97e38b316fc28c6c4394.png

我细品了这段文字,发现我以前的想法错了。之前认为这个文件路径可能是在项目路径中(做web项目的原因),但是看这个解释应该是把模拟器当作真机,那真正要操作的文件应该在模拟器中文件夹中才对。跟着路径找我自己的电脑。

4c4e84f0e186bf55e555fbe2f8d0cba8.png

bin目录中只有一个Simulator的执行文件并没有file_system\是我找错了还是官方小哥哥说错了???然后我

又陷入了迷茫,最终我做了一个大胆的决定,手动把目录创建出来。

e041c00d26573cdad01ad52fa520b536.png

9b7bc6414bcc30b163ef77c00627f0c9.png

妙啊~大家去看看自己的Sdk\previewer\3.0.0.80\liteWearable\bin\ 里边有没有file_system文件。有真机的也可以用真机试试~

最后贴上几个API的运行代码和结果,有几个测试还是有问题,不知道是不是姿势不对~

import file from '@system.file';

export default {

data: {},

onShow() {

//将指定文件移动到其他指定位置。有问题,不能移动

file.move({

srcUri: 'internal://app/a.txt',

dstUri: 'internal://app/pac',

success: function(uri) {

console.log('file.move>>>');

},

fail: function(data, code) {

console.error('file.move>>>fail-->>>' + code + ', data: ' + data);

},

});

//将指定文件拷贝并存储到指定位置

file.copy({

srcUri: 'internal://app/a.txt',

dstUri: 'internal://app/b.txt',

success: function(uri) {

console.log('file.copy>>>');

},

fail: function(data, code) {

console.error('file.copy>>>fail-->>>' + code + ', data: ' + data);

},

});

//获取指定路径下全部文件的列表

file.list({

uri: 'internal://app',

success: function (data) {

console.log('file.list>>>' + JSON.stringify(data));

},

fail: function (data, code) {

console.log('file.list>>>fail-->>>' + data + ', code: ' + code);

}

});

//获取指定本地文件的信息

file.get({

uri: 'internal://app/pac',

recursive: true,

success: function (data) {

console.log('file.get>>>' + JSON.stringify(data));

},

fail: function (data, code) {

console.error('file.get>>>fail-->>>' + code + ', data: ' + data);

},

})

//写文本内容到指定文件。

file.writeText({

uri: 'internal://app/pac/xxx.txt',

text: 'Text that just for test.',

encoding:'UTF-8',

append:true,

success: function() {

console.log('file.writeText>>>');

},

fail: function(data, code) {

console.error('file.writeText>>>fail-->>>' + code + ', data: ' + data);

},

});

//从指定文件中读取文本内容,读取失败

file.readText({

uri: 'internal://app/pac/xxx.txt',

encoding:'UTF-8',

position:3,

length:10,

success: function(data) {

console.log('file.readText>>>' + data.text);

},

fail: function(data, code) {

console.error('file.readText>>>fail-->>>' + code + ', data: ' + data);

},

});

//判断指定文件或目录是否存在,success也没有个返回值??????

file.access({

uri: 'internal://app/pac/xxx.txt',

success: function() {

console.log('file.access>>>');

},

fail: function(data, code) {

console.error('file.access>>>fail-->>>' + code + ', data: ' + data);

},

});

//创建指定目录。不支持创建文件哦。

file.mkdir({

uri: 'internal://app/sss/ttt',

recursive:true,

success: function() {

console.log('file.mkdir>>>');

},

fail: function(data, code) {

console.error('file.mkdir>>>fail-->>>' + code + ', data: ' + data);

},

});

//删除指定目录。

file.rmdir({

uri: 'internal://app/eee',

recursive:true,

success: function() {

console.log('file.rmdir>>>');

},

fail: function(data, code) {

console.error('file.rmdir>>>fail-->>>' + code + ', data: ' + data);

},

});

//删除本地文件,删除不掉

file.delete({

uri: 'internal://app/del.txt',

success: function() {

console.log('file.delete>>>');

},

fail: function(data, code) {

console.error('file.delete>>>fail-->>>' + code + ', data: ' + data);

},

});

}

}

==========================================================

11/06 00:28:42: Launching com.example.huxi

[Info] Application onCreate

[Error] file.move>>>fail-->>>200, data: System error

[Debug] file.copy>>>

[Debug] file.list>>>{"fileList":[{"uri":"a.txt","length":0,"lastModifiedTime":0,"type":"file"},{"uri":"

[Debug] a.txt.bak","length":0,"lastModifiedTime":0,"type":"file"},{"uri":"b.txt","length":0,"lastModifi

[Debug] edTime":0,"type":"file"},{"uri":"del.txt","length":0,"lastModifiedTime":0,"type":"file"},{"uri"

[Debug] :"pac","length":-1,"lastModifiedTime":0,"type":"dir"},{"uri":"sss","length":-1,"lastModifiedTim

[Debug] e":0,"type":"dir"}]}

[Debug] file.get>>>{"subFiles":[{"uri":"uuu.txt","length":0,"lastModifiedTime":0,"type":"file"},{"uri":

[Debug] "xxx.txt","length":0,"lastModifiedTime":0,"type":"file"}]}

[Debug] file.writeText>>>

[Error] file.readText>>>fail-->>>200, data: System error

[Debug] file.access>>>

[Debug] file.mkdir>>>

[Error] file.rmdir>>>fail-->>>301, data: File or directory not exist

[Error] file.delete>>>fail-->>>301, data: File or directory not exist

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值