FileUtil.ets
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import fs from '@ohos.file.fs';
import buffer from '@ohos.buffer';
// 大小和单位
const GB_MAGNITUDE: number = 1024 * 1024 * 1024
const MB_MAGNITUDE: number = 1024 * 1024
const KB_MAGNITUDE: number = 1024
const GB_SYMBOL: string = 'GB'
const MB_SYMBOL: string = 'MB'
const KB_SYMBOL: string = 'KB'
const BYTE_SYMBOL: string = 'B'
export class FileUtil {
/**
* 新建并打开文件
*/
static createOrOpen(path: string) : fs.File{
let isExist = fs.accessSync(path);
let file: fs.File;
if(isExist) {
file = fs.openSync(path, fs.OpenMode.READ_WRITE);
}else {
file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE)
}
return file;
}
/**
* 保存arrayBuffer到文件
* @param path
* @param arrayBuffer
* @returns
*/
static writeBufferToFile(path: string, arrayBuffer: ArrayBuffer): number {
try {
let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
let value = fs.writeSync(file.fd, arrayBuffer);
fs.closeSync(file);
return value;
}catch (err){
console.log("FileUtil", "writeFile err:" + err);
return -1;
}
}
/**
* 保存文本到文件
* @param path
* @param text
* @returns
*/
static writeStrToFile(path: string, text: string): number {
try {
let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
let value = fs.writeSync(file.fd, text);
fs.closeSync(file);
return value;
}catch (err) {
console.log("FileUtil", "writeFile err:" + err);
return -1;
}
}
}
export class CommonConstants {
/**
* ShowToast duration.
*/
static readonly SHOW_TOAST_DURATION: number = 4000;
/**
* ShowToast bottom.
*/
static readonly SHOW_TOAST_BOTTOM: number = 108;
/**
* Image size.
*/
static readonly IMAGE_SIZE: number = 200;
/**
* The full percentage of component.
*/
static readonly FULL_PERCENT: string = '100%';
/**
* The ninety percent of the components.
*/
static readonly NINETY_PERCENT: string = '90%';
/**
* The seventy percent of the components.
*/
static readonly SEVENTY_PERCENT: string = '70%';
/**
* The fifteen percent of the bottom of the margin.
*/
static readonly FIFTEEN_PERCENT: string = '15%';
}