Why i cant read json file rom resource folder with Kotlin?
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
json kotlin
|
show 3 more comments
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
json kotlin
Possible duplicate of How to read a text file from resources in Kotlin?
– pavlos163
Nov 22 at 21:06
it dosent work, when i tried it, it says that file is too long .. ?
– mac123
Nov 22 at 21:07
What is the exact error?
– pavlos163
Nov 22 at 21:10
I updated the question with the error log
– mac123
Nov 22 at 21:12
Hm, that's strange. Can you show the code you tried after looking at the above link?
– pavlos163
Nov 22 at 21:14
|
show 3 more comments
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
json kotlin
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
json kotlin
json kotlin
edited Nov 22 at 21:12
asked Nov 22 at 21:01
mac123
457
457
Possible duplicate of How to read a text file from resources in Kotlin?
– pavlos163
Nov 22 at 21:06
it dosent work, when i tried it, it says that file is too long .. ?
– mac123
Nov 22 at 21:07
What is the exact error?
– pavlos163
Nov 22 at 21:10
I updated the question with the error log
– mac123
Nov 22 at 21:12
Hm, that's strange. Can you show the code you tried after looking at the above link?
– pavlos163
Nov 22 at 21:14
|
show 3 more comments
Possible duplicate of How to read a text file from resources in Kotlin?
– pavlos163
Nov 22 at 21:06
it dosent work, when i tried it, it says that file is too long .. ?
– mac123
Nov 22 at 21:07
What is the exact error?
– pavlos163
Nov 22 at 21:10
I updated the question with the error log
– mac123
Nov 22 at 21:12
Hm, that's strange. Can you show the code you tried after looking at the above link?
– pavlos163
Nov 22 at 21:14
Possible duplicate of How to read a text file from resources in Kotlin?
– pavlos163
Nov 22 at 21:06
Possible duplicate of How to read a text file from resources in Kotlin?
– pavlos163
Nov 22 at 21:06
it dosent work, when i tried it, it says that file is too long .. ?
– mac123
Nov 22 at 21:07
it dosent work, when i tried it, it says that file is too long .. ?
– mac123
Nov 22 at 21:07
What is the exact error?
– pavlos163
Nov 22 at 21:10
What is the exact error?
– pavlos163
Nov 22 at 21:10
I updated the question with the error log
– mac123
Nov 22 at 21:12
I updated the question with the error log
– mac123
Nov 22 at 21:12
Hm, that's strange. Can you show the code you tried after looking at the above link?
– pavlos163
Nov 22 at 21:14
Hm, that's strange. Can you show the code you tried after looking at the above link?
– pavlos163
Nov 22 at 21:14
|
show 3 more comments
1 Answer
1
active
oldest
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)
}
}
Thanx a lot! :)
– mac123
Nov 22 at 21:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437935%2fwhy-i-cant-read-json-file-rom-resource-folder-with-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
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)
}
}
Thanx a lot! :)
– mac123
Nov 22 at 21:33
add a comment |
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)
}
}
Thanx a lot! :)
– mac123
Nov 22 at 21:33
add a comment |
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)
}
}
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)
}
}
answered Nov 22 at 21:19
pavlos163
481743
481743
Thanx a lot! :)
– mac123
Nov 22 at 21:33
add a comment |
Thanx a lot! :)
– mac123
Nov 22 at 21:33
Thanx a lot! :)
– mac123
Nov 22 at 21:33
Thanx a lot! :)
– mac123
Nov 22 at 21:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437935%2fwhy-i-cant-read-json-file-rom-resource-folder-with-kotlin%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of How to read a text file from resources in Kotlin?
– pavlos163
Nov 22 at 21:06
it dosent work, when i tried it, it says that file is too long .. ?
– mac123
Nov 22 at 21:07
What is the exact error?
– pavlos163
Nov 22 at 21:10
I updated the question with the error log
– mac123
Nov 22 at 21:12
Hm, that's strange. Can you show the code you tried after looking at the above link?
– pavlos163
Nov 22 at 21:14