[英]In Android Studio, pasting Java code into a Kotlin file doesn't convert to Kotlin anymore
[英]How to convert this code of Java to Kotlin for Android Studio for retrieving data of a file?
我搜索了如何在 Kotlin 中檢索文件,並在 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();
}
}
}
}
如何用 Kotlin 語言寫這個...? 到目前為止,我已嘗試將其轉換為 Kotlin 但在某些行上仍然存在一些錯誤(用 表示)。 請幫助我解決這些錯誤。
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())
}
試試下面的代碼
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()
}
}
}
只需將您的 java 代碼粘貼到 Android Studio.kt 文件中,它會建議您將其轉換為 Kotlin。 在那里你得到了 99% 的 kotlin 代碼。 然后更改一些缺失的元素。
我的解決方案是:
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()
}
}
看起來您在那里有無限循環,所以我將其更改為 for each loop 以讀取 InputStream 中的所有行
這是我的解決方案:
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()
}
}
這應該是相當等價的。 請注意if((text = br.readLine() != null) {
不能直接寫在 Kotlin 中,但是你可以用while(true) { break;
替換它。
如果您使用的是.use {
,則不需要手動close()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.