dds dds - 1 month ago 10
Android Question

How can I store my files for reading later in Android

I am doing a quastion game like Bilioner game but its about a history lesson and there will be history questions and subject expressions, too. I think to store my questions in a file and take from this file. And my subject texts too. I dont use database(sqlite) I want to do it with file. I cant store in strings because they are long. I am writing them to a txt file and I can read them from there. With code below:

try {
FileOutputStream fileout=openFileOutput("abc.txt", MODE_PRIVATE);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write("asddddddddddddddddddddddddddddddddddddd");
outputWriter.close();

//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();

} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "error!",
Toast.LENGTH_SHORT).show();
}


try {
FileInputStream fileIn=openFileInput("abc.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);

char[] inputBuffer= new char[1000];
String s="";
int charRead;

while ((charRead=InputRead.read(inputBuffer))>0) {
// char to string conversion
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
Toast.makeText(getBaseContext(), s,
Toast.LENGTH_SHORT).show();

}
InputRead.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "error!",
Toast.LENGTH_SHORT).show();

}


But I dont want to write to txt file. Because I cant write the data EVERYTÄ°ME. I want to just read them from an existing txt file. But I cant read without writing. Where should I place my txt file in Android app folder and how can I find them and after read?

Answer

Place it in res/raw folder. Then you can get an InputStream:

InputStream ins = getResources().openRawResource(
        getResources().getIdentifier("file",
        "raw", getPackageName()));
Comments