Repository navigation
Expand file tree
/
Copy pathFileIOUtils.kt
More file actions
389 lines (366 loc) · 9.9 KB
/
Copy pathFileIOUtils.kt
File metadata and controls
389 lines (366 loc) · 9.9 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.excellence.basetoolslibrary.utils
import com.excellence.basetoolslibrary.utils.EmptyUtils.isEmpty
import java.io.*
import java.nio.charset.Charset
/**
* <pre>
* author : VeiZhang
* blog : http://tiimor.cn
* time : 2022/4/7
* desc : 文件流相关
* </pre>
*/
object FileIOUtils {
private const val BUF_SIZE = 8 * 1024
/**
* 将字符串写入文件
*
* @param file
* @param content
* @param append
* @return
*/
@JvmStatic
fun writeFile(file: File?, content: String?, append: Boolean): Boolean {
try {
if (file == null || isEmpty(content)) {
return false
}
FileUtils.createNewFile(file)
val writer = BufferedWriter(FileWriter(file, append))
writer.write(content)
writer.close()
return true
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
/**
* 将字符串写入文件
*
* @param path
* @param content
* @param append
* @return
*/
@JvmStatic
fun writeFile(path: String?, content: String?, append: Boolean): Boolean {
if (isEmpty(path)) {
return false
}
return writeFile(File(path), content, append)
}
/**
* 将字节数组写入文件
*
* @param file
* @param bytes
* @param append
* @return
*/
@JvmStatic
fun writeFile(file: File?, bytes: ByteArray?, append: Boolean): Boolean {
try {
if (file == null || isEmpty(bytes)) {
return false
}
FileUtils.createNewFile(file)
val os: OutputStream = BufferedOutputStream(FileOutputStream(file, append))
os.write(bytes)
os.close()
return true
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
/**
* 将字节数组写入文件
*
* @param path
* @param bytes
* @param append
* @return
*/
@JvmStatic
fun writeFile(path: String?, bytes: ByteArray?, append: Boolean): Boolean {
if (isEmpty(path)) {
return false
}
return writeFile(File(path), bytes, append)
}
/**
* 将输入流写入文件里
*
* @param file
* @param stream
* @param append
* @param isCloseable 是否关闭输入流
* @return
*/
@JvmStatic
fun writeFile(file: File?, stream: InputStream?, append: Boolean, isCloseable: Boolean): Boolean {
try {
if (file == null || stream == null) {
return false
}
FileUtils.createNewFile(file)
val os: OutputStream = BufferedOutputStream(FileOutputStream(file, append))
val buf = ByteArray(BUF_SIZE)
var len: Int
while (stream.read(buf, 0, buf.size).also { len = it } != -1) {
os.write(buf, 0, len)
}
os.close()
if (isCloseable) {
stream.close()
}
return true
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
/**
* 将输入流写入文件里
*
* @param path
* @param stream
* @param append
* @param isCloseable 是否关闭输入流
* @return
*/
@JvmStatic
fun writeFile(path: String?, stream: InputStream?, append: Boolean, isCloseable: Boolean): Boolean {
if (isEmpty(path)) {
return false
}
return writeFile(File(path), stream, append, isCloseable)
}
/**
* 读取输入流为字节数组
*
* @param stream
* @return
*/
@JvmStatic
fun readFile2Bytes(stream: InputStream?): ByteArray? {
try {
val os = ByteArrayOutputStream()
val buf = ByteArray(BUF_SIZE)
var len: Int
while (stream!!.read(buf, 0, buf.size).also { len = it } != -1) {
os.write(buf, 0, len)
}
stream.close()
os.close()
return os.toByteArray()
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return null
}
/**
* 读取文件为字节数组
*
* @param file
* @return
*/
@JvmStatic
fun readFile2Bytes(file: File?): ByteArray? {
try {
if (!FileUtils.isFileExists(file)) {
return null
}
val stream: InputStream = FileInputStream(file)
return readFile2Bytes(stream)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return null
}
/**
* 读取文件为字节数组
*
* @param path
* @return
*/
@JvmStatic
fun readFile2Bytes(path: String?): ByteArray? {
if (isEmpty(path)) {
return null
}
return readFile2Bytes(File(path))
}
/**
* 读取输入流为字符串
*
* @param stream
* @param charset
* @return
*/
@JvmStatic
fun readFile2String(stream: InputStream?, charset: String?): String {
try {
if (isEmpty(stream)) {
return ""
}
val bytes = readFile2Bytes(stream)!!
if (isEmpty(charset)) {
return String(bytes)
} else {
return String(bytes, Charset.forName(charset))
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return ""
}
/**
* 读取文件为字符串
*
* @param file
* @param charset UTF-8、GBK...
* @return
*/
@JvmStatic
fun readFile2String(file: File?, charset: String?): String {
try {
if (!FileUtils.isFileExists(file)) {
return ""
}
val stream: InputStream = FileInputStream(file)
return readFile2String(stream, charset)
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return ""
}
/**
* 读取文件为字符串
*
* @param path
* @param charset UTF-8、GBK...
* @return
*/
@JvmStatic
fun readFile2String(path: String?, charset: String?): String {
if (isEmpty(path)) {
return ""
}
return readFile2String(File(path), charset)
}
/**
* 拷贝文件
*
* @param stream
* @param os
* @return
*/
@JvmStatic
fun copyFile(stream: InputStream?, os: OutputStream?): Boolean {
try {
if (stream == null || os == null) {
return false
}
val buf = ByteArray(BUF_SIZE)
var len: Int
while (stream.read(buf, 0, buf.size).also { len = it } != -1) {
os.write(buf, 0, len)
}
stream.close()
os.close()
return true
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
/**
* 拷贝文件
*
* @param sourceFile
* @param targetFile
* @return
*/
@JvmStatic
fun copyFile(sourceFile: File?, targetFile: File?): Boolean {
if (!FileUtils.isFileExists(sourceFile)) {
return false
}
try {
return copyFile(FileInputStream(sourceFile), FileOutputStream(targetFile))
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
/**
* 拷贝文件
* 1.起始位置
* 2.截止长度,最大为原始文件长度;<=0,表示不截取
*
* @param sourceFile
* @param targetFile
* @param skip
* @param targetSize
* @return
*/
@JvmStatic
fun copyFile(sourceFile: File, targetFile: File?, skip: Long, targetSize: Long): Boolean {
if (!FileUtils.isFileExists(sourceFile)) {
return false
}
try {
val stream: InputStream = FileInputStream(sourceFile)
val os: OutputStream = FileOutputStream(targetFile)
stream.skip(skip)
if (targetSize > 0 && targetSize < sourceFile.length()) {
if (targetSize <= BUF_SIZE) {
val buf = ByteArray(targetSize.toInt())
var len: Int
if (stream.read(buf, 0, buf.size).also { len = it } != -1) {
os.write(buf, 0, len)
}
} else {
val buf = ByteArray(BUF_SIZE)
var len: Int
var count: Long = 0
while (stream.read(buf, 0, buf.size).also { len = it } != -1) {
os.write(buf, 0, len)
count += len.toLong()
if (targetSize + BUF_SIZE > count) {
len = stream.read(buf, 0, buf.size)
os.write(buf, 0, len)
count += len.toLong()
}
if (targetSize <= count) {
break
}
}
}
stream.close()
os.close()
return true
} else {
return copyFile(stream, os)
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return false
}
/**
* 拷贝文件,截止长度,最大为原始文件长度;<=0,表示不截取
*
* @param sourceFile
* @param targetFile
* @param targetSize
* @return
*/
@JvmStatic
fun copyFile(sourceFile: File, targetFile: File?, targetSize: Long): Boolean {
return copyFile(sourceFile, targetFile, 0, targetSize)
}
}