# How to copy file from assets to the app database folder?

**URL:** <https://discuss.kotlinlang.org/t/how-to-copy-file-from-assets-to-the-app-database-folder/14484>\
**Category:** Uncategorized\
**Created:** [October 1, 2019, 4:06am UTC](https://discuss.kotlinlang.org/t/how-to-copy-file-from-assets-to-the-app-database-folder/14484 "2019-10-01T04:06:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ipinsao](https://avatars.discourse-cdn.com/v4/letter/i/58956e/32.png) [@ipinsao](https://discuss.kotlinlang.org/u/ipinsao)\
**Post date:** [October 1, 2019, 4:06am UTC](https://discuss.kotlinlang.org/t/how-to-copy-file-from-assets-to-the-app-database-folder/14484/1 "2019-10-01T04:06:14Z")

</div>

I’m new to Kotlin I’m just trying to copy a database file from the assets folder but I keep getting a filenotfound exception. The code is below thanks. I checked double checked the paths. I have another version in java which has no problem.

```
@Throws(IOException::class)
private fun copyDataBase() {

    val outFileName = DATABASE_PATH + DATABASE_NAME

    val myOutput = FileOutputStream(outFileName)
    val myInput = this.assets.open(DATABASE_NAME)

    val buffer = ByteArray(1024)
    var length: Int = myInput.read(buffer)
    while ((length) > 0) {
        myOutput.write(buffer, 0, length)
        length = myInput.read(buffer)
    }
    myInput.close()
    myOutput.flush()
    myOutput.close()
}

```

---

<div class="post-metadata">

**Author:** ![xduke](https://avatars.discourse-cdn.com/v4/letter/x/3da27b/32.png) [@xduke](https://discuss.kotlinlang.org/u/xduke)\
**Post date:** [October 2, 2019, 1:01pm UTC](https://discuss.kotlinlang.org/t/how-to-copy-file-from-assets-to-the-app-database-folder/14484/2 "2019-10-02T13:01:03Z")

</div>

You already asked on Stackoverflow, right?

> <https://stackoverflow.com/questions/58177921/kotlin-how-to-copy-file-from-assets-folder-to-databases-folder>

From this code snippet I can’t say why it doesn’t work.

- You could show the value of outFileName.
- You could also show the “equivalent” Java code.
- You could also show the exact exception message and trace

Note

- Javadoc of FileOutputStream constructor:  
`FileNotFoundException` - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
- the code is not idiomatic kotlin (nor would it even be clean java code).

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [October 2, 2019, 4:57pm UTC](https://discuss.kotlinlang.org/t/how-to-copy-file-from-assets-to-the-app-database-folder/14484/3 "2019-10-02T16:57:06Z")

</div>

> [@xduke](#):
>
> the code is not idiomatic kotlin (nor would it even be clean java code).

I don’t really see anything wrong with this code. So if you make a statement such as this, you should at least back it up. (Ok the `this.` in `this.assets.open` is not needed but other than that I don’t see anything wrong)

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [October 2, 2019, 5:10pm UTC](https://discuss.kotlinlang.org/t/how-to-copy-file-from-assets-to-the-app-database-folder/14484/4 "2019-10-02T17:10:29Z")

</div>

Cleanup should happen in a finally block. Idiomatic kotlin would use the `use` method. kotlin also provides `copyTo` method for streams.

Non idiomatic code is not the cause of the exception. Some possibilities are that a path is wrong, the destination folder isn’t created, or the path is to external storage and that permission hasn’t been granted. Hard to tell without more info like a stack trace.
