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()
}
}
}
File(FILE_NAME).readLines().fold(StringBuilder()){ builder, next ->
builder.appendln(next)
}.also {
mEditText.setText(it.toString())
}
Try below code
fun load(v: View, fileName: String ) {
var fis = File(fileName).inputStream()
try {
fis = openFileInput(fileName)
var isr = InputStreamReader(fis)
val br = BufferedReader(isr)
val sb = StringBuilder()
var text:List<String> = br.readLines() //Do not use this function for huge files.
while (true) {
sb.append(text).append("\n")
}
mEditText?.text(sb.toString()) //mEditText is your EditText
} catch (e : FileNotFoundException) {
e.printStackTrace()
} catch (e : IOException) {
e.printStackTrace()
} finally {
try {
fis.close()
} catch (e : IOException) {
e.printStackTrace()
}
}
}
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.
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
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()
}
}
}
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 {
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.