From 2bfe563eaf1d7ab57e88644bd552e11b78538c6b Mon Sep 17 00:00:00 2001 From: zhoulisheng1 Date: Thu, 30 Mar 2023 15:21:37 +0800 Subject: [PATCH] =?UTF-8?q?1.=E9=80=82=E9=85=8D=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84SDK,fileio=E6=8E=A5=E5=8F=A3=E5=BA=9F=E5=BC=83?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8fs=E6=8E=A5=E5=8F=A3=E4=BB=A3?= =?UTF-8?q?=E6=9B=BF=202.fix=20=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E5=A6=82=E6=9E=9C=E6=96=87=E4=BB=B6=E4=B8=8D=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E8=B0=83=E7=94=A8fs.unlinkSync=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BC=9A=E5=8F=91=E7=94=9F=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhoulisheng1 --- .../main/ets/components/cache/FileReader.ets | 6 +- .../main/ets/components/cache/FileUtils.ets | 63 ++++++++++--------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/imageknife/src/main/ets/components/cache/FileReader.ets b/imageknife/src/main/ets/components/cache/FileReader.ets index 10969e0..854971d 100644 --- a/imageknife/src/main/ets/components/cache/FileReader.ets +++ b/imageknife/src/main/ets/components/cache/FileReader.ets @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import fileio from '@ohos.fileio'; +import fs from '@ohos.file.fs'; export class FileReader { @@ -32,8 +32,8 @@ export class FileReader { return } try { - this.stream = fileio.createStreamSync(path, 'r+'); - var stat = fileio.statSync(path) + this.stream = fs.createStreamSync(path, 'r+'); + var stat = fs.statSync(path) this.fileLength = stat.size } catch (e) { } diff --git a/imageknife/src/main/ets/components/cache/FileUtils.ets b/imageknife/src/main/ets/components/cache/FileUtils.ets index c5754d3..4a6af85 100644 --- a/imageknife/src/main/ets/components/cache/FileUtils.ets +++ b/imageknife/src/main/ets/components/cache/FileUtils.ets @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import resmgr from '@ohos.resourceManager' -import fileio from '@ohos.fileio'; + +import fs from '@ohos.file.fs'; export class FileUtils { base64Str: string= '' @@ -37,7 +37,7 @@ export class FileUtils { * @return number 文件句柄id */ createFile(path: string): number{ - return fileio.openSync(path, 0o100, 0o666) + return fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).fd } /** @@ -45,7 +45,14 @@ export class FileUtils { * @param path 文件绝对路径及文件名 */ deleteFile(path: string):void { - fileio.unlinkSync(path); + try { + let fileExist = fs.accessSync(path); + if(fileExist) { + fs.unlinkSync(path); + } + }catch (err){ + console.log("FileUtils deleteFile Method has error, err msg="+err.message + " err code="+err.code); + } } /** @@ -54,7 +61,7 @@ export class FileUtils { */ deleteFolderSync(path: string):void { if (this.existFolder(path)) { - fileio.rmdirSync(path); + fs.rmdirSync(path); } } @@ -64,7 +71,7 @@ export class FileUtils { */ deleteFolderAsync(path: string, deleteComplete, deleteError) { if (this.existFolder(path)) { - fileio.rmdir(path) + fs.rmdir(path) .then(deleteComplete).catch(deleteError); } } @@ -74,14 +81,14 @@ export class FileUtils { * @param path 文件绝对路径及文件名 */ copyFile(oriPath: string, newPath: string) { - fileio.copyFileSync(oriPath, newPath); + fs.copyFileSync(oriPath, newPath); } /** * 清空已有文件数据 */ clearFile(path: string):number { - return fileio.openSync(path, 0o1000) + return fs.openSync(path, fs.OpenMode.TRUNC).fd } /** @@ -89,11 +96,11 @@ export class FileUtils { */ writeFile(path: string, content: ArrayBuffer | string) { try { - let fd = fileio.openSync(path, 0o102, 0o666) - fileio.ftruncateSync(fd) - fileio.writeSync(fd, content) - fileio.fsyncSync(fd) - fileio.closeSync(fd) + let fd = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).fd + fs.truncateSync(fd) + fs.writeSync(fd, content) + fs.fsyncSync(fd) + fs.closeSync(fd) } catch (e) { console.log("FileUtils - Failed to writeFile for " + e) } @@ -105,18 +112,18 @@ export class FileUtils { writeData(path: string, content: ArrayBuffer | string) { try { console.info("FileUtils - writeData size 1= " + path) - let fd = fileio.openSync(path, 0o102, 0o666) + let fd = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).fd console.info("FileUtils - writeData size 2= ") - let stat = fileio.statSync(path) + let stat = fs.statSync(path) console.info("FileUtils - writeData size = " + stat.size) - fileio.writeSync(fd, content, { position: stat.size }) + fs.writeSync(fd, content, { offset: stat.size }) let length = 0 if (content instanceof ArrayBuffer) { length = content.byteLength } else { length = content.length } - fileio.closeSync(fd) + fs.closeSync(fd) } catch (e) { console.log("FileUtils - Failed to writeData for " + e) } @@ -127,7 +134,7 @@ export class FileUtils { */ exist(path: string): boolean{ try { - let stat = fileio.statSync(path) + let stat = fs.statSync(path) return stat.isFile() } catch (e) { console.debug("FileUtils - fileutils exsit e" + e) @@ -151,7 +158,7 @@ export class FileUtils { */ getFileSize(path: string): number{ try { - let stat = fileio.statSync(path) + let stat = fs.statSync(path) return stat.size } catch (e) { console.error("FileUtils - FileUtils getFileSize e " + e) @@ -164,14 +171,14 @@ export class FileUtils { */ readFilePic(path: string): ArrayBuffer { try { - let stat = fileio.statSync(path) + let stat = fs.statSync(path) console.info("FileUtils - readFilePic 1") - let fd = fileio.openSync(path, 0o2); - let length = fileio.statSync(path).size + let fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd; + let length = fs.statSync(path).size console.info("FileUtils - readFilePic 2 length = " + length) let buf = new ArrayBuffer(length); console.info("FileUtils - readFilePic 3") - fileio.readSync(fd, buf) + fs.readSync(fd, buf) return buf } catch (e) { console.log("FileUtils - readFilePic " + e) @@ -185,10 +192,10 @@ export class FileUtils { */ readStream(path: string): string { try { - let stat = fileio.statSync(path) + let stat = fs.statSync(path) let length = stat.size let buf = new ArrayBuffer(length); - let ss = fileio.createStreamSync(path, "r+"); + let ss = fs.createStreamSync(path, "r+"); ss.readSync(buf) ss.closeSync(); return String.fromCharCode.apply(null, new Uint8Array(buf)) @@ -206,7 +213,7 @@ export class FileUtils { console.error("FileUtils - writeStream =1 ") this.createFile(path) console.error("FileUtils - writeStream 2 ") - let ss = fileio.createStreamSync(path, "r+"); + let ss = fs.createStreamSync(path, "r+"); console.error("FileUtils - writeStream 3 " + tempArray.byteLength) let num = ss.writeSync(tempArray, { encoding: 'utf-8' @@ -226,7 +233,7 @@ export class FileUtils { createFolder(path: string) { //创建文件夹 if (!this.existFolder(path)) { - fileio.mkdirSync(path) + fs.mkdirSync(path) } } @@ -236,7 +243,7 @@ export class FileUtils { */ existFolder(path: string): boolean{ try { - let stat = fileio.statSync(path) + let stat = fs.statSync(path) return stat.isDirectory() } catch (e) { console.debug("fileutils folder exsit error=" + e)