riku
2021-06-30 5353617c7b2135ab00f98d8e05b2f8dfb2e096ed
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
package com.flightfeather.uav.common.utils
 
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.text.SimpleDateFormat
import java.util.*
 
/**
 * @author riku
 * Date: 2019/9/11
 */
class FileUtil {
 
    private var file: File
    private var basePath:String = "${File.separator}UAVData${File.separator}"
    private var closeThread: Thread? = null
    private var fw: FileWriter? = null
    private var bw: BufferedWriter? = null
    private var oldTime: Date
 
    init {
        val fileName = "data-${SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(Date())}.txt"
        val path = "$basePath$fileName"
        file = File(path)
        oldTime = Date()
    }
 
    companion object{
        var instance: FileUtil? = null
        get() {
            if (field == null) {
                field = FileUtil()
            }
            return field
        }
    }
 
    fun saveObdData(str: String) {
        //检查文档是否存在
        if (!file.exists()) {
            file.parentFile.mkdirs()
            println("----创建文件夹:${file.parentFile.absolutePath}")
            file.createNewFile()
            println("----创建文件:${file.absolutePath}")
        }
        //文件最大512Kb,超过后新建文档
        if (file.length() + str.toByteArray().size > 512 * 1024 || TimeUtil.isNextDay(oldTime, Date())) {
            //超过一天后,更新当前时间
            oldTime = Date()
 
            val fileName = "data-${SimpleDateFormat("yyyy-MM-dd-hh-mm-ss").format(Date())}.txt"
            val path = "$basePath$fileName"
            file = File(path)
            file.createNewFile()
 
            //关闭原有输出流
            bw?.run {
                flush()
                close()
            }
            fw?.run {
                flush()
                close()
            }
            //新建输出流
            fw = FileWriter(file, true)
            bw = BufferedWriter(fw)
        }
        //第一次写文档时初始化输出流
        if (bw == null || fw == null) {
            fw = FileWriter(file, true)
            bw = BufferedWriter(fw)
        }
 
        bw?.run {
            write(str)
            newLine()
            flush()
        }
 
        readyToShutDownStream(bw, fw)
        println("----写入完成")
 
    }
 
    private fun readyToShutDownStream(bw: BufferedWriter?, fw:FileWriter?) {
        //终端上一次的计时线程
        closeThread?.interrupt()
        closeThread = Thread(Runnable {
            //2分钟没有新的数据写入就关闭输出流
            try {
                Thread.sleep(20 * 1000)
            } catch (e: Throwable) {
            }
            bw?.run {
                close()
            }
            this.bw = null
            fw?.run {
                close()
            }
            this.fw = null
        }).also {
            it.start()
        }
    }
}