深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
需要这份系统化的资料的朋友,可以戳这里获取
@State message: string = ‘HomePage’
@State msg: string = ‘’;
onPageShow() {
build() {
Row() {
Column() {
}
}
import fs,{ Filter } from ‘@ohos.file.fs’;
import Logger from ‘…/utils/Logger’;
const TAG = ‘[FileUtil]’;
const context = getContext(this);
- 创建文件,写入文件,并返回文件内容
- @returns
*/
export function createFile(): string {
// 获取应用文件路径
// let context = getContext(this) as common.UIAbilityContext;
// filesDir为 /data/storage/el2/base/haps/entry/files
let filesDir = context.filesDir;
let filePath = filesDir + ‘/test.txt’
// 新建并打开文件
let file = fs.openSync(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入一段内容至文件
let writeLen = fs.writeSync(file.fd, “Try to write str.”);
console.info("The length of str is: " + writeLen);
Logger.info(TAG, "The length of str is: " + writeLen);
// // 从文件读取一段内容,方式一
// let buf = new ArrayBuffer(1024);
// let readLen = fs.readSync(file.fd, buf, { offset: 0 });
// let content = String.fromCharCode.apply(null, new Uint8Array(buf.slice(0, readLen)));
// Logger.info(TAG, "readSync the content of file: " + content);
// 从文件读取内容,方式二
let content = fs.readTextSync(filePath);
Logger.info(TAG, "readTextSync the content of file: " + content);
// 关闭文件
fs.closeSync(file);
return content;
}
export function readWriteFile() {
// 获取应用文件路径
// let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
// 打开文件
let srcFile = fs.openSync(filesDir + ‘/test.txt’, fs.OpenMode.READ_WRITE);
let destFile = fs.openSync(filesDir + ‘/destFile.txt’, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 读取源文件内容并写入至目的文件
let bufSize = 4096;
let readSize = 0;
let buf = new ArrayBuffer(bufSize);
let readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });
while (readLen > 0) {
readSize += readLen;
fs.writeSync(destFile.fd, buf);
readLen = fs.readSync(srcFile.fd, buf, { offset: readSize });
}
// 关闭文件
fs.closeSync(srcFile);
fs.closeSync(destFile);
Logger.info(TAG, “readWriteFile success”);
export async function readWriteFileWithStream() {
// 获取应用文件路径
// let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
// 打开文件流
let inputStream = fs.createStreamSync(filesDir + ‘/test.txt’, ‘r+’);
let outputStream = fs.createStreamSync(filesDir + ‘/destFile.txt’, “w+”);
// 以流的形式读取源文件内容并写入目的文件
let bufSize = 4096;
let readSize = 0;
let buf = new ArrayBuffer(bufSize);
let readLen = await inputStream.read(buf, { offset: readSize });
readSize += readLen;
while (readLen > 0) {
await outputStream.write(buf);
readLen = await inputStream.read(buf, { offset: readSize });
readSize += readLen;
}
// 关闭文件流
inputStream.closeSync();
outputStream.closeSync();
}
- 查看文件列表
- @returns 文件名[]
*/
export function getFileList(): string[] {
// 获取应用文件路径
// let context = getContext(this) as common.UIAbilityContext;
let filesDir = context.filesDir;
// 查看文件列表
// let options = {
// recursion: false,
// listNum: 0,
// filter: {
// suffix: [‘.png’, ‘.jpg’, ‘.txt’], // 匹配文件后缀名为’.png’,‘.jpg’,‘.txt’
// //displayName: [‘test*’], // 匹配文件全名以’test’开头
// fileSizeOver: 0, // 匹配文件大小大于等于0
// lastModifiedAfter: new Date(0).getTime(), // 匹配文件最近修改时间在1970年1月1日之后
// },
// }
class ListFileOption {
public recursion: boolean = false;
public listNum: number = 0;
public filter: Filter = {};
}
let option = new ListFileOption();
option.filter.suffix = [‘.png’, ‘.jpg’, ‘.txt’]; // 匹配文件后缀名为’.png’,‘.jpg’,‘.txt’
option.filter.displayName = [‘test*’]; // 匹配文件全名以’test’开头
option.filter.fileSizeOver = 0; // 匹配文件大小大于等于0
option.filter.lastModifiedAfter = new Date(0).getTime(); // 匹配文件最近修改时间在1970年1月1日之后
let files = fs.listFileSync(filesDir, option);
for (let i = 0; i < files.length; i++) {
console.info();
}
return files;
}
import common from ‘@ohos.app.ability.common’;
import { createFile, readWriteFile, getFileList } from ‘…/utils/FileUtil’;
import Logger from ‘…/utils/Logger’;
const TAG = ‘[FilePage]’;
@Entry
@Component
struct FilePage {
@State message: string = ‘Hello World’
// ets文件中获取上下文
private context = getContext(this) as common.UIAbilityContext;
build() {
Row() {
Column() {
Button(‘新建并读写文件’)
.onClick(() => {