HarmonyOS鸿蒙Next中使用fs.OpenMode.CREATE报错13900002 - No such file or directory

HarmonyOS鸿蒙Next中使用fs.OpenMode.CREATE报错13900002 - No such file or directory 【问题描述】:使用fs.OpenMode.CREATE 报错13900002 - No such file or directory

【问题现象】:使用fs.OpenMode.CREATE 报错13900002 - No such file or directory,描述是不存在这个文件或目录,我这边用fs.OpenMode.CREATE创建,为什么还会报这个错误。

相关链接:13900002

【版本信息】:6.0.2

【复现代码】:

import { fileUri, picker } from "@kit.CoreFileKit";
import fs from '@ohos.file.fs';

@Entry
@Component
struct Main {
  build() {
    Button("创建文件").onClick(() => {
      createPublicFile(`测试${new Date().getTime()}`, "测试", "docx")
    })
  }
}

export async function createPublicFile(fileName: string, content: string, suffix: string) {
  try {
    const documentViewPicker = new picker.DocumentViewPicker()
    const result = await documentViewPicker.save({ pickerMode: picker.DocumentPickerMode.DOWNLOAD })
    const filePath = new fileUri.FileUri(result[0]).getFullDirectoryUri()
    const fullPath = `${filePath}/${fileName}.${suffix}`;
    console.log('测试'+fullPath)
    const file = fs.openSync(fullPath, fs.OpenMode.CREATE);
    await fs.write(file.fd, content);
    fs.closeSync(file);
  } catch (error) {
    const err = error as BusinessError;
    console.error(`错误信息:${err.code} - ${err.message}`);
  }
}

【尝试解决方案】:暂无


更多关于HarmonyOS鸿蒙Next中使用fs.OpenMode.CREATE报错13900002 - No such file or directory的实战教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

您好,13900002 报错码标识没有这个文件或目录,需要确认是否存在’file://docs/storage/Users/currentUser/Download/com.example.myapplication/测试1770953926114.docx’这个路径,若不存在可以通过fs.mkdirSync创建目录,fs.OpenMode.CREATE只能创建文件,不能创建目录

可参考以下步骤:

  1. 使用fs.access判断文件/目录是否存在;
  2. 使用fs.mkdir创建文件夹(当参数recursion指定为true时,可递归创建多文件夹);
  3. 使用方fs.open(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)创建文件并以读写模式打开;
  4. 文件读写则使用fs.read、fs.write方法。

具体可以参考文档如何在应用沙箱内创建文件或者文件夹

更多关于HarmonyOS鸿蒙Next中使用fs.OpenMode.CREATE报错13900002 - No such file or directory的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


file://docs/storage/Users/currentUser/Download/com.example.myapplication/测试1770953926114.docx,这个路径不存在。file://docs/storage/Users/currentUser/Download/com.example.myapplication这个路径存在,在这个路径下创建的测试1770953926114.docx文件。报错,

您好,可以参考

import { fileUri, picker } from "@kit.CoreFileKit";
import { fileIo as fs } from '@kit.CoreFileKit';

@Entry
@Component
struct Main {
  build() {
    Button("创建文件").onClick(() => {
      createPublicFile()
    })
  }
}

export async function createPublicFile() {
  // 创建文件保存选项实例
  const documentSaveOptions = new picker.DocumentSaveOptions();
  documentSaveOptions.pickerMode = picker.DocumentPickerMode.DOWNLOAD; // 设置为下载模式
  // 创建文件选择器实例
  const documentViewPicker = new picker.DocumentViewPicker();

  documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
    //获取路径
    const filePath = documentSaveResult[0];
    console.log(filePath)
    // `测试${new Date().getTime()}
    const fullPath = new fileUri.FileUri(filePath + `/测试${new Date().getTime()}.docx`).path;
    console.log(fullPath)
    const file = fs.openSync(fullPath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
    fs.writeSync(file.fd, 'Hello World!');
    
    fs.closeSync(file.fd);
  }).catch((err: BusinessError) => {
    console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
  })
}

使用fileIo工具的create系列方法试试。创建时加上OpenMode.Write,

该错误表明文件路径不存在。在HarmonyOS NEXT中,fs.OpenMode.CREATE 模式要求其父目录必须已存在。请先使用 fs.mkdir 或 fs.mkdirSync 创建完整的目录路径,然后再使用 CREATE 模式打开文件。

这个错误通常是因为文件路径中的目录不存在。fs.OpenMode.CREATE 只能创建文件本身,但不会自动创建不存在的父目录。

在你的代码中,filePath 是通过 getFullDirectoryUri() 获取的目录路径,但用户选择的下载目录可能在实际文件系统中并不存在。

解决方案是在调用 fs.openSync() 之前,确保目录存在:

import { fileUri, picker } from "@kit.CoreFileKit";
import fs from '@ohos.file.fs';

async function createPublicFile(fileName: string, content: string, suffix: string) {
  try {
    const documentViewPicker = new picker.DocumentViewPicker()
    const result = await documentViewPicker.save({ pickerMode: picker.DocumentPickerMode.DOWNLOAD })
    const filePath = new fileUri.FileUri(result[0]).getFullDirectoryUri()
    const fullPath = `${filePath}/${fileName}.${suffix}`;
    
    // 关键修复:确保目录存在
    const dirPath = filePath.toString();
    try {
      await fs.access(dirPath);
    } catch {
      // 目录不存在,创建它
      await fs.mkdir(dirPath, { recursive: true });
    }
    
    console.log('测试'+fullPath)
    const file = fs.openSync(fullPath, fs.OpenMode.CREATE);
    await fs.write(file.fd, content);
    fs.closeSync(file);
  } catch (error) {
    const err = error as BusinessError;
    console.error(`错误信息:${err.code} - ${err.message}`);
  }
}

主要修改:

  1. 使用 fs.access() 检查目录是否存在
  2. 如果目录不存在,使用 fs.mkdir() 创建目录,设置 recursive: true 确保创建所有必要的父目录

这样就能确保在创建文件之前,文件所在的目录已经存在,从而避免 13900002 错误。

回到顶部