0

I have searched over how to retrieve a file in Kotlin, and I found this code in Java.

public void load(View v) {
    FileInputStream fis = null;

    try {
        fis = openFileInput(FILE_NAME);
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader br = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String text;

        while ((text = br.readLine()) != null) {
            sb.append(text).append("\n");
        }

        mEditText.setText(sb.toString());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

How to write this in Kotlin Language...? So far by myself, I have tried to convert it in Kotlin but there are still some errors on some lines (denoted by ). Please help me in solving these errors.

var fis = File(FILE_NAME).inputStream     //<ror>Unresolved reference File 
try {
    fis = openFileInput(FILE_NAME)
    var isr = InputStreamReader(fis)
    val br = BufferedReader(isr)
    val sb = StringBuilder()
    var text:List<String> = br.readLines()

    while ((text) != null) {                //<ror>
        sb.append(text).append("\n")
    }

    mEditText?.text(sb.toString())          //<ror>

} catch (e : FileNotFoundException) {
    e.printStackTrace()
} catch (e : IOException) {
    e.printStackTrace()
} finally {
    if (fis != null) {
        try {
            fis.close()
        } catch (e : IOException) {
            e.printStackTrace()
        }
    }
}

5 Answers 5

1
File(FILE_NAME).readLines().fold(StringBuilder()){ builder, next ->
   builder.appendln(next)
}.also {
   mEditText.setText(it.toString())
}

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

Comments

0

My solution is:

var fis = File(FILE_NAME).inputStream()                             

    try {
        fis = openFileInput(FILE_NAME)
        val isr = InputStreamReader(fis)
        val br = BufferedReader(isr)
        val sb = StringBuilder()
        var lines = br.readLines()

        for (line in lines) {                                       
            sb.append(line).append("\n")
        }

        mEditText?.setText(sb.toString())                           

    } catch (e : FileNotFoundException) {
        e.printStackTrace()
    } catch (e : IOException) {
        e.printStackTrace()
    } finally {
        try {
            fis.close()
        } catch (e : IOException) {
            e.printStackTrace()
        }

    }

It looks like you have infinite loop there so I change it to for each loop to read all the lines from the InputStream

Comments

0

Just paste your java code in Android Studio .kt file, It will suggest you to convert it into Kotlin. There you get 99% of the kotlin code. Then change some missing elements.

1 Comment

i know... i did that already but it didn't work.... that's i posted my first question here ~
0

Here is my solution:

fun load(v: View?) {
    var fis: FileInputStream? = null

    try {
        fis = openFileInput(FILE_NAME)
        val isr = InputStreamReader(fis)
        val br = BufferedReader(isr)
        val lines = br.readLines()
        val output = lines.joinToString("\n")

        mEditText.setText(output)
    } catch (e: FileNotFoundException) {
        e.printStackTrace()
    } catch (e: IOException) {
        e.printStackTrace()
    } finally {
        try {
            fis?.close()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

Comments

0
fun void load(v: View) {
    try {
        openFileInput(FILE_NAME).use { fis ->
            val br = BufferedReader(InputStreamReader(fis))

            var text: String? = null

            while(true) {
                text = br.readLine() ?: break
                sb.append(text).append("\n")
            }

            editText.setText(sb.toString())
        }
    } catch(e: Exception) {
        e.printStackTrace()
    }
}

This should be reasonably equivalent. Please note that the if((text = br.readLine() != null) { cannot be written like that directly in Kotlin, however you can replace it with a while(true) { break;.

Manual close() is not needed if you are using .use {.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.