1.适配新版本的SDK,fileio接口废弃,使用fs接口代替

2.fix 删除文件,如果文件不存在调用fs.unlinkSync接口会发生崩溃

Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
zhoulisheng1 2023-03-30 15:21:37 +08:00
parent 44d10f5e90
commit 2bfe563eaf
2 changed files with 38 additions and 31 deletions

View File

@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import fileio from '@ohos.fileio'; import fs from '@ohos.file.fs';
export class FileReader { export class FileReader {
@ -32,8 +32,8 @@ export class FileReader {
return return
} }
try { try {
this.stream = fileio.createStreamSync(path, 'r+'); this.stream = fs.createStreamSync(path, 'r+');
var stat = fileio.statSync(path) var stat = fs.statSync(path)
this.fileLength = stat.size this.fileLength = stat.size
} catch (e) { } catch (e) {
} }

View File

@ -12,8 +12,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import resmgr from '@ohos.resourceManager'
import fileio from '@ohos.fileio'; import fs from '@ohos.file.fs';
export class FileUtils { export class FileUtils {
base64Str: string= '' base64Str: string= ''
@ -37,7 +37,7 @@ export class FileUtils {
* @return number 文件句柄id * @return number 文件句柄id
*/ */
createFile(path: string): number{ 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 文件绝对路径及文件名 * @param path 文件绝对路径及文件名
*/ */
deleteFile(path: string):void { 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 { deleteFolderSync(path: string):void {
if (this.existFolder(path)) { if (this.existFolder(path)) {
fileio.rmdirSync(path); fs.rmdirSync(path);
} }
} }
@ -64,7 +71,7 @@ export class FileUtils {
*/ */
deleteFolderAsync(path: string, deleteComplete, deleteError) { deleteFolderAsync(path: string, deleteComplete, deleteError) {
if (this.existFolder(path)) { if (this.existFolder(path)) {
fileio.rmdir(path) fs.rmdir(path)
.then(deleteComplete).catch(deleteError); .then(deleteComplete).catch(deleteError);
} }
} }
@ -74,14 +81,14 @@ export class FileUtils {
* @param path 文件绝对路径及文件名 * @param path 文件绝对路径及文件名
*/ */
copyFile(oriPath: string, newPath: string) { copyFile(oriPath: string, newPath: string) {
fileio.copyFileSync(oriPath, newPath); fs.copyFileSync(oriPath, newPath);
} }
/** /**
* 清空已有文件数据 * 清空已有文件数据
*/ */
clearFile(path: string):number { 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) { writeFile(path: string, content: ArrayBuffer | string) {
try { try {
let fd = fileio.openSync(path, 0o102, 0o666) let fd = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE).fd
fileio.ftruncateSync(fd) fs.truncateSync(fd)
fileio.writeSync(fd, content) fs.writeSync(fd, content)
fileio.fsyncSync(fd) fs.fsyncSync(fd)
fileio.closeSync(fd) fs.closeSync(fd)
} catch (e) { } catch (e) {
console.log("FileUtils - Failed to writeFile for " + e) console.log("FileUtils - Failed to writeFile for " + e)
} }
@ -105,18 +112,18 @@ export class FileUtils {
writeData(path: string, content: ArrayBuffer | string) { writeData(path: string, content: ArrayBuffer | string) {
try { try {
console.info("FileUtils - writeData size 1= " + path) 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= ") console.info("FileUtils - writeData size 2= ")
let stat = fileio.statSync(path) let stat = fs.statSync(path)
console.info("FileUtils - writeData size = " + stat.size) console.info("FileUtils - writeData size = " + stat.size)
fileio.writeSync(fd, content, { position: stat.size }) fs.writeSync(fd, content, { offset: stat.size })
let length = 0 let length = 0
if (content instanceof ArrayBuffer) { if (content instanceof ArrayBuffer) {
length = content.byteLength length = content.byteLength
} else { } else {
length = content.length length = content.length
} }
fileio.closeSync(fd) fs.closeSync(fd)
} catch (e) { } catch (e) {
console.log("FileUtils - Failed to writeData for " + e) console.log("FileUtils - Failed to writeData for " + e)
} }
@ -127,7 +134,7 @@ export class FileUtils {
*/ */
exist(path: string): boolean{ exist(path: string): boolean{
try { try {
let stat = fileio.statSync(path) let stat = fs.statSync(path)
return stat.isFile() return stat.isFile()
} catch (e) { } catch (e) {
console.debug("FileUtils - fileutils exsit e" + e) console.debug("FileUtils - fileutils exsit e" + e)
@ -151,7 +158,7 @@ export class FileUtils {
*/ */
getFileSize(path: string): number{ getFileSize(path: string): number{
try { try {
let stat = fileio.statSync(path) let stat = fs.statSync(path)
return stat.size return stat.size
} catch (e) { } catch (e) {
console.error("FileUtils - FileUtils getFileSize e " + e) console.error("FileUtils - FileUtils getFileSize e " + e)
@ -164,14 +171,14 @@ export class FileUtils {
*/ */
readFilePic(path: string): ArrayBuffer { readFilePic(path: string): ArrayBuffer {
try { try {
let stat = fileio.statSync(path) let stat = fs.statSync(path)
console.info("FileUtils - readFilePic 1") console.info("FileUtils - readFilePic 1")
let fd = fileio.openSync(path, 0o2); let fd = fs.openSync(path, fs.OpenMode.READ_WRITE).fd;
let length = fileio.statSync(path).size let length = fs.statSync(path).size
console.info("FileUtils - readFilePic 2 length = " + length) console.info("FileUtils - readFilePic 2 length = " + length)
let buf = new ArrayBuffer(length); let buf = new ArrayBuffer(length);
console.info("FileUtils - readFilePic 3") console.info("FileUtils - readFilePic 3")
fileio.readSync(fd, buf) fs.readSync(fd, buf)
return buf return buf
} catch (e) { } catch (e) {
console.log("FileUtils - readFilePic " + e) console.log("FileUtils - readFilePic " + e)
@ -185,10 +192,10 @@ export class FileUtils {
*/ */
readStream(path: string): string { readStream(path: string): string {
try { try {
let stat = fileio.statSync(path) let stat = fs.statSync(path)
let length = stat.size let length = stat.size
let buf = new ArrayBuffer(length); let buf = new ArrayBuffer(length);
let ss = fileio.createStreamSync(path, "r+"); let ss = fs.createStreamSync(path, "r+");
ss.readSync(buf) ss.readSync(buf)
ss.closeSync(); ss.closeSync();
return String.fromCharCode.apply(null, new Uint8Array(buf)) return String.fromCharCode.apply(null, new Uint8Array(buf))
@ -206,7 +213,7 @@ export class FileUtils {
console.error("FileUtils - writeStream =1 ") console.error("FileUtils - writeStream =1 ")
this.createFile(path) this.createFile(path)
console.error("FileUtils - writeStream 2 ") console.error("FileUtils - writeStream 2 ")
let ss = fileio.createStreamSync(path, "r+"); let ss = fs.createStreamSync(path, "r+");
console.error("FileUtils - writeStream 3 " + tempArray.byteLength) console.error("FileUtils - writeStream 3 " + tempArray.byteLength)
let num = ss.writeSync(tempArray, { let num = ss.writeSync(tempArray, {
encoding: 'utf-8' encoding: 'utf-8'
@ -226,7 +233,7 @@ export class FileUtils {
createFolder(path: string) { createFolder(path: string) {
//创建文件夹 //创建文件夹
if (!this.existFolder(path)) { if (!this.existFolder(path)) {
fileio.mkdirSync(path) fs.mkdirSync(path)
} }
} }
@ -236,7 +243,7 @@ export class FileUtils {
*/ */
existFolder(path: string): boolean{ existFolder(path: string): boolean{
try { try {
let stat = fileio.statSync(path) let stat = fs.statSync(path)
return stat.isDirectory() return stat.isDirectory()
} catch (e) { } catch (e) {
console.debug("fileutils folder exsit error=" + e) console.debug("fileutils folder exsit error=" + e)