为什么我不能用 Kotlin 读取 json 文件 rom 资源文件夹?

3 json kotlin

我试图从我的 json 文件中读取,我在 root 中拥有它,但是当我打包到 jar 时,该文件没有附带。现在我将 json 文件放在“资源”文件夹中。

我的代码:

@Component
class DefaultData {

@Autowired
private lateinit var gameOfThronesService: GameOfThronesService

@PostConstruct
fun initializeDefault() {
    val reader = JsonReader(FileReader("game-of-thrones.json"))
    val gameofthronesCharacters: List<GameOfThronesDto> = Gson().fromJson(reader, object : TypeToken<List<GameOfThronesDto>>() {}.type)

    println("-----> JSON Data <-----")
    gameofthronesCharacters.forEach{ println(it) }

    gameOfThronesService.createCharactersFromJson(gameofthronesCharacters)
}
}
Run Code Online (Sandbox Code Playgroud)

当我在 root 中有 json 文件时这有效,但在“资源”文件夹中找不到它,如何解决这个问题?

我也试过:How to read a text file from resources in Kotlin?

然后我收到以下错误:

(File name too long)
at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_181]
at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_181]
at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_181]
at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[na:1.8.0_181]
at java.io.FileReader.<init>(FileReader.java:58) ~[na:1.8.0_181]
at com.ahmmud16.gameofthrones.util.DefaultData.initializeDefault(DefaultData.kt:24) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:309) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]
... 18 common frames omitted
Run Code Online (Sandbox Code Playgroud)

pav*_*163 6

要从资源文件夹中读取文件,您应该在此处遵循此答案。你试过了,但方法不对。

执行此操作时: val fileContent = this::class.java.classLoader.getResource("game-of-thrones.json").readText(),您正在阅读文件的内容。然后您将整个文件内容传递给FileReader就好像它是文件名一样(这就是您收到File name too long错误的原因)。

从我在 Gson 文档中看到的内容来看,您可以完全跳过 a 的创建JsonReader,并将 a 传递StringfromJson().

您可以尝试以下操作:

@Component
class DefaultData {

@Autowired
private lateinit var gameOfThronesService: GameOfThronesService

@PostConstruct
fun initializeDefault() {
    val fileContent = this::class.java.classLoader.getResource("game-of-thrones.json").readText()

    val gameofthronesCharacters: List<GameOfThronesDto> = Gson().fromJson(fileContent, object : TypeToken<List<GameOfThronesDto>>() {}.type)

    println("-----> JSON Data <-----")
    gameofthronesCharacters.forEach{ println(it) }

    gameOfThronesService.createCharactersFromJson(gameofthronesCharacters)
}
}
Run Code Online (Sandbox Code Playgroud)