Forráskód Böngészése

提交人:jtm
提交内容:读取文件编码乱码问题

江天明 1 éve
szülő
commit
3cea2ae76f

+ 1 - 1
BusinessCommon/src/main/java/com/develop/common/utils/AppVersionUtil.kt

@@ -274,7 +274,7 @@ object AppVersionUtil {
             if (!jsonFile.exists()) {
                 return
             }
-            val jsonContent = FileKit.readFileToString(jsonFile)
+            val jsonContent = FileKit.readFileToStringB(jsonFile)
             val contentData = jsonDecoder.decodeFromString<RecipeDataConfig>(jsonContent)
             // contentData.resetAllCodes()
             FoodDataProvider.getDatabase().runInTransaction {

+ 1 - 1
BusinessStep/src/main/java/com/develop/step/viewmodel/CookDetailViewModel.kt

@@ -200,7 +200,7 @@ class CookDetailViewModel : BaseViewModel() {
                 }
                 return
             }
-            val jsonContent = FileKit.readFileToString(jsonFile)
+            val jsonContent = FileKit.readFileToStringB(jsonFile)
 
             val contentData = GsonUtils.fromJson(jsonContent, RecipeDataConfig::class.java)
 //            contentData.resetAllCodes()

+ 24 - 1
libBase/src/main/java/com/develop/base/util/FileKit.kt

@@ -4,11 +4,11 @@ import android.content.ContentUris
 import android.content.Context
 import android.database.Cursor
 import android.net.Uri
-import android.os.Build
 import android.os.Environment
 import android.provider.DocumentsContract
 import android.provider.MediaStore
 import java.io.*
+import java.nio.charset.Charset
 
 
 object FileKit {
@@ -54,6 +54,29 @@ object FileKit {
         return sb.toString()
     }
 
+    fun readFileToStringB(file: File): String {
+        val sb = StringBuilder()
+        try {
+            //打开文件输入流
+            val inputStream = FileInputStream(file)
+            val inputStreamReader = InputStreamReader(inputStream,Charset.forName("utf-8"))
+            val bufferedReader = BufferedReader(inputStreamReader)
+            var str: String
+            while (bufferedReader.readLine().also { str = it } != null) {
+                println("====>字符流:$str")
+                sb.append(str)
+            }
+            bufferedReader.close()
+            //关闭输入流
+            inputStreamReader.close()
+            inputStream.close()
+        } catch (ex: Exception) {
+            ex.printStackTrace()
+        }
+        return sb.toString()
+    }
+
+
     fun writeToFile(file: File, content: String) {
         try {
             val out = BufferedWriter(FileWriter(file))