HarmonyOS 怎么能从picker选择的fileUri,使用readline?

怎么能从picker选择的fileUri,使用readline?

直接用fileUri、FileUri.path都说参数不对。

代码:

let file=fs.openSync(fileUri,fs.OpenMode.READ_ONLY)
let fileSize = fs.statSync(file.fd).size;
if (fileSize == 0) {
  return { error: ImportResultError.fileEmpty, total: 0, insert: 0 }
}

let result: ImportResult = { total: 0, insert: 0 };
let csvNameIndex: CsvNameIndex | undefined;
let charCount = 0;
let batchArray: LocationEntity[] = [];

let options: Options = { encoding: 'utf-8' };
// let readIterator = fs.readLinesSync(fileUri, options);
let readIterator = fs.readLinesSync(file.path, options);

报错:

05-29 11:04:38.846 25129-25129 C04388/file_api com.xxx E [read_lines.cpp:192->Sync] Failed to read lines of the file, error: 
05-29 11:04:38.846 25129-25129 C03f00/ArkCompiler com.xxx E [ecmascript] Pending exception before IsMixedDebugEnabled called in line:3200, exception details as follows:
05-29 11:04:38.846 25129-25129 C03f00/ArkCompiler com.xxx E Error: No such file or directory
05-29 11:04:38.846 25129-25129 C03f00/ArkCompiler com.xxx E at importCSVFile (app/src/main/ets/pages/inout/helper/ImportHelper.ets:44:28)
05-29 11:04:38.846 25129-25129 C03f00/ArkCompiler com.xxx E at anonymous (app/src/main/ets/pages/inout/InOutPage.ets:265:13)
HarmonyOS
7天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

我们提供的demo的意思是首先在应用的沙箱里边创建一个文件,然后复制到,选择的样机的文件管理里边的一个路径下面,我们的readline方法现在不支持读取文件管理里边的文件,所以只能用read去读取,readline参数是沙箱路径,无法读取文件管理路径中的文件,可以试试readSync方法,下面是demo:

import fs, { Options } from '@ohos.file.fs';
import { common } from '@kit.AbilityKit';
import { picker } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  private appContext: common.Context = getContext(this);
  private fileDir: string = ''
  @State resultFileDir: string = ''

  createFile(){
    let cacheDir = this.appContext.cacheDir;
    this.fileDir = cacheDir + '/HelloWorldlee.txt'
    let file = fs.openSync(this.fileDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
    let writeLen = fs.writeSync(file.fd, '你好世界!');
    console.info("write data to file succeed and size is:" + writeLen);
    fs.closeSync(file)
  }

  async saveFile(){
    try {
      let documentSaveOptions = new picker.DocumentSaveOptions();
      documentSaveOptions.newFileNames = ['HelloWorld.txt'];
      let documentPicker = new picker.DocumentViewPicker();
      await documentPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
        let uri = documentSaveResult[0];
        this.resultFileDir = uri
        let sanFile = fs.openSync(this.fileDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
        let pubFile = fs.openSync(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
        fs.copyFileSync(sanFile.fd, pubFile.fd)

        fs.close(sanFile)
        fs.close(pubFile)
        console.info('DocumentViewPicker.save successfully, documentSaveResult uri: ' + JSON.stringify(documentSaveResult));
      }).catch((err: BusinessError) => {
        console.error('DocumentViewPicker.save failed with err: ' + JSON.stringify(err));
      });
      let pubFile = fs.openSync(this.resultFileDir, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
      let buf = new ArrayBuffer(4096);
      fs.readSync(pubFile.fd, buf);
    } catch (error) {
      let err: BusinessError = error as BusinessError;
      console.error('DocumentViewPicker failed with err: ' + JSON.stringify(err));
    }
  }

  build() {
    Row() {
      Column() {
        Button(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(()=>{
            this.createFile();
            this.saveFile();
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
7天前
相关问题
HarmonyOS 怎么能够设置禁止分屏?
393浏览 • 1回复 待解决
HarmonyOS picker选择数据源问题
125浏览 • 1回复 待解决
HarmonyOS picker使用问题确认
367浏览 • 1回复 待解决