0

I have a two-dimensional array of objects that I store in a file in the Assets folder of an Android Studio project. The file is serialized and named "maindata.conv"

Reading the file in Java is a breeze:

static public Object[][] mainTable = null; ObjectInputStream instream = new ObjectInputStream(getAssets().open("maindata.Conv")); mainTable = (Object[][]) instream.readObject(); 

But when I translate the code to Kotlin, the translated code does not work:

val instream = ObjectInputStream(assets.open("maindata.Conv")) MainActivity.mainTable = instream.readObject() as Array<Array<Any?>?> 

The word "open" is flagged in red as "unresolved". Nothing I try to import works. How do I read a raw file in Kotlin from the Assets folder? Please help.

2
  • try assets. `open` instead of assets.open as open is a Kotlin keyword. Commented Oct 25, 2021 at 22:38
  • In Kotlin assets is just a shorthand for getAssets() which is a method on a Context - so if you call that inside an Activity it will just work. The Java code is the same so you must be calling it from a different place in the Kotlin version? Commented Oct 25, 2021 at 22:40

1 Answer 1

0

Thanks but I found the root of my problem. I was trying to execute the Kotlin code from within a Java project. When I created a Kotlin project, the code works just fine. My guess is that it may have something to do with the context of the activity, but I am not 100% sure.

Sign up to request clarification or add additional context in comments.

2 Comments

You can mix and match Kotlin and Java in the same project - you just weren't calling assets on a Context (e.g. inside an activity instance). If you're getting a red underline it's because assets isn't a recognised property on whatever this is in your scope
Correct! Thanks! I'm still getting to know Kotlin.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.