1.适配新版本的SDK,fileio接口废弃,使用fs接口代替
2.fix 删除文件,如果文件不存在调用fs.unlinkSync接口会发生崩溃 Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
44d10f5e90
commit
2bfe563eaf
|
@ -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) {
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue