-
-
Save laaptu/6459577 to your computer and use it in GitHub Desktop.
public String ReadFromfile(String fileName, Context context) { | |
StringBuilder ReturnString = new StringBuilder(); | |
InputStream fIn = null; | |
InputStreamReader isr = null; | |
BufferedReader input = null; | |
try { | |
fIn = context.getResources().getAssets() | |
.open(fileName); | |
isr = new InputStreamReader(fIn); | |
input = new BufferedReader(isr); | |
String line = ""; | |
while ((line = input.readLine()) != null) { | |
ReturnString.append(line); | |
} | |
} catch (Exception e) { | |
e.getMessage(); | |
} finally { | |
try { | |
if (isr != null) | |
isr.close(); | |
if (fIn != null) | |
fIn.close(); | |
if (input != null) | |
input.close(); | |
} catch (Exception e2) { | |
e2.getMessage(); | |
} | |
} | |
return ReturnString.toString(); | |
} |
READING IN DATA FROM A TEXT FILE USING KOTLIN AND ANDROID STUDIO 3.3.2
I use Android Studio version 3.3.2 with the latest version of kotlin installed. I am a relative beginner to Android / Kotlin programming, although I have a strong background in C programming. I needed to find a way of reading data from a text file and spent ages trying all sorts of suggestions off the internet as to how to do this simple task. All of the methods suggested did not work for me, I suspect because they were contributions from java programmers. Just one method came close (thanks to the contributor!) and after some modification I had a working app.I hope I can save some time for someone else in a similar position with the following contribution. I'm sure expert programmers will think of better ways of doing things, but ... I KNOW FOR A FACT THAT THIS ONE WORKS!!! The program teads a text file called 'friends' which is contained in the directory app/src/main/assets/friends Note that the directory 'assets' needs to be created and the text file copied into it. I believe that it is expected that the filename should all be in lower case with no extension of .txt for example. I created a TextView window called 'Results' and made it scrollable to see the data in the whole window.
The file <MainActivity.kt> is as follows :-
package com.android.filereadtest
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val dictfile = getString(R.string.TestFile)
try {
val fIn = (assets.open(dictfile))
val file = InputStreamReader (fIn)
val br = BufferedReader (file)
var line = br.readLine ()
val all = StringBuilder ()
while (line!= null) {
all.append (line + "\n")
line = br.readLine ()
}
br.close ()
file.close ()
Results.text = all
} catch (e: IOException) {
Toast.makeText (this, " Could not read the file called < $dictfile > ", Toast.LENGTH_LONG) .show ()
}
}
}
The file activity_main.xml is as follows:-
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:scrollbars="vertical"
android:scrollbarStyle="insideInset" app:layout_constraintHeight_max="500dp"
app:layout_constraintWidth_max="300dp" android:id="@+id/ScrollWin">
<TextView
android:layout_width="300dp"
android:layout_height="600dp"
android:id="@+id/Results"
app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"
android:textSize="15sp" android:textStyle="bold"/>
</ScrollView>
</android.support.constraint.ConstraintLayout>
GOOD LUCK! Hope this example of reading a text file saves someone else some time!
Malcolm, Cornwall, UK
This is kind of shorter (Kotlin):