Android Studio 3.6
在build.gradle中:
compileOptions {
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_8
}
api 'org.apache.commons:commons-io:1.3.2'
字符串
在我的代码中
import org.apache.commons.io.FileUtils
import java.io.*
import java.nio.charset.StandardCharsets
val file = File(folderPath + File.separator + resultFileName)
FileUtils.writeStringToFile(
file,
fileContents,
StandardCharsets.UTF_8.toString()
)
型
但我得到错误:
java.nio.charset.CharsetICU[UTF-8]
java.io.UnsupportedEncodingException: java.nio.charset.CharsetICU[UTF-8]
at java.nio.charset.Charset.forNameUEE(Charset.java:322)
at java.lang.String.getBytes(String.java:534)
at org.apache.commons.io.IOUtils.write(IOUtils.java:810)
at org.apache.commons.io.FileUtils.writeStringToFile(FileUtils.java:1110)
at com.myproject.client.common.util.AndroidFileUtil.saveTextToFile(AndroidFileUtil.kt:106)
at com.myproject.client.service.RecognizedCheckDataService$Companion.saveRecognizedText(RecognizedCheckDataService.kt:117)
at com.myproject.client.viewmodel.ScanCheckRompetrolViewModel.finishProcessRecognizedCheck(ScanCheckRompetrolViewModel.kt:1059)
at com.myproject.client.viewmodel.ScanCheckRompetrolViewModel.access$finishProcessRecognizedCheck(ScanCheckRompetrolViewModel.kt:28)
at com.myproject.client.viewmodel.ScanCheckRompetrolViewModel$runDetector$1.onSuccess(ScanCheckRompetrolViewModel.kt:193)
at com.myproject.client.viewmodel.ScanCheckRompetrolViewModel$runDetector$1.onSuccess(ScanCheckRompetrolViewModel.kt:28)
at com.google.android.gms.tasks.zzn.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.nio.charset.IllegalCharsetNameException: java.nio.charset.CharsetICU[UTF-8]
型
2条答案
按热度按时间b5buobof1#
Charset#toString()
没有很好地指定;在Android上,它返回"java.nio.charset.CharsetICU[UTF-8]"
作为找到here。当然,该字符串不是现有字符集的名称。但是根本不需要
toString()
调用。writeStringToFile
方法有一个直接接受Charset
的重载。lndjwyie2#
在我的例子中,用
"UTF-8"
替换StandardCharsets.UTF_8.toString()
解决了这个问题。请注意,
toString()
返回name()
的结果,因此用name()
替换它,正如对该问题的评论所建议的那样,不会改变任何东西。