3
votes

im trying to read from my json file, i had it in root, but when i package to jar, the file dont come with. Now i put the json file in "resource" folder.

My code:

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

This worked when i had the json file in root, but it cant find it in "resource" folder, how to solve this?

I also tried: How to read a text file from resources in Kotlin?

Then i get this following error:

(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
it dosent work, when i tried it, it says that file is too long .. ?user9883549
What is the exact error?pavlos163
I updated the question with the error loguser9883549
Hm, that's strange. Can you show the code you tried after looking at the above link?pavlos163

1 Answers

6
votes

To read a file from the resources folder, you should be following this answer here. You tried it, but not in the correct way.

When doing this: val fileContent = this::class.java.classLoader.getResource("game-of-thrones.json").readText(), you are reading the contents of the file. You are then passing the whole file content to FileReader as if it was the file name (that's why you're getting a File name too long error).

From what I'm seeing in the Gson documentation, you can skip the creating of a JsonReader completely, and pass a String to fromJson().

Can you try the following:

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