Reputation: 319
I am trying to make an android app using android studio. My app needs to read a text file line by line, and perform a task on the string on each line. I am using bufferedread for this, but I am constantly getting a File not found error. I created an assets folder in /app/src/main/assets/ and I have copied the text file into this assets folder. Any help will be greatly appreciated! Please find my code below :-
package com.example.spandanmadan1.hinglish;
import android.content.res.AssetManager;
import android.provider.UserDictionary;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MyActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button bsubmit = (Button) findViewById(R.id.Hinglishbutton);
bsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AssetManager assManager = getApplicationContext().getAssets();
InputStream fis = assManager.open("HindiSlang.txt");
BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));
String line=null;
while ((line = bfr.readLine()) != null) {
UserDictionary.Words.addWord(getApplicationContext(),line,1,"",null);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_my, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 4
Views: 7564
Reputation: 4631
In Kotlin, I used this way for simple text such as Titles:
val fileName = "list100.txt"
val inputString = application.assets.open(fileName).bufferedReader().use { it.readText() }
val list100: List<String> = inputString.split("\n")
Don't forget add your file in the Assets folder!
Upvotes: -1
Reputation: 31
Here's a Kotlin example for reading an InputStream (or File) line-by-line:
InputStream.bufferedReader().useLines { lines ->
lines.forEach { line ->
System.out.println(line)
}
}
Upvotes: 3
Reputation: 3850
Try one of the following:
try {
fIn = getApplicationContext().getResources().getAssets()
.open("HindiSlang.txt", Context.MODE_WORLD_READABLE);
input = new BufferedReader(new InputStreamReader(fIn));
String line = "";
while ((line = input.readLine()) != null) {
// process the line..
}
} catch (Exception e) {
e.getMessage();
}
OR write a function:
public String LoadData(String inFile) {
String tContents = "";
try {
InputStream stream = getAssets().open(inFile); // you will get the method getAssets anywhere from current activity.
int size = stream.available();
byte[] buffer = new byte[size];
stream.read(buffer);
stream.close();
tContents = new String(buffer);
} catch (IOException e) {
// Handle exceptions here
}
return tContents;
}
Upvotes: 4