Android Kotlin open asset file

Viewed 7305

I want to open asset file. Before the java code is worked but When I was change code to kotlin it doesn't work.

Java code its work

        InputStream  streamIN = new BufferedInputStream(context.getAssets().open(Database.ASSET));
        OutputStream streamOU = new BufferedOutputStream(new FileOutputStream(LOCATION));
        byte[] buffer = new byte[1024];
        int length;
        while ((length = streamIN.read(buffer)) > 0) {
            streamOU.write(buffer, 0, length);
        }

        streamIN.close();
        streamOU.flush();
        streamOU.close();

I change code to Kotlin but It doesn't work

    var length: Int
    val buffer = ByteArray(1024)
    BufferedOutputStream(FileOutputStream(LOCATION)).use {
        out ->
        {
            BufferedInputStream(context.assets.open(Database.ASSET)).use {
                length = it.read(buffer)
                if (length > 0) out.write(buffer, 0, length)
            }

            out.flush()
        }
    }
1 Answers
Related