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

时间:2018-11-22 21:01:09

标签: json kotlin

我试图从我的json文件中读取

im,我把它放在根目录下,但是当我打包成jar时,该文件就没有了。现在,我将json文件放在“ resource”文件夹中。

我的代码:

@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)
}
}

当我在根目录中有json文件时,此方法有效,但是在“ resource”文件夹中找不到该文件,如何解决此问题?

我也尝试过: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

1 个答案:

答案 0 :(得分:2)

要从资源文件夹中读取文件,应遵循以下答案here。您尝试过,但是方法不正确。

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

根据我在Gson文档中看到的内容,您可以完全跳过创建JsonReader,并将String传递给fromJson()

您可以尝试以下方法吗?

@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)
}
}