Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read file from the assets folder

I have a large text file that is in the assets folder, and I have a button that reads the file's next line successfully. However, I want to read the previous line in case the user clicks another button.

Reading the whole file to memory is not an option. The file lines are not numbered.

like image 890
Ruyonga Dan Avatar asked Mar 18 '26 04:03

Ruyonga Dan


1 Answers

InputStream is = getResources().getAssets().open("abc.txt");
String result= convertStreamToString(is);

public static String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();
        char[] buffer = new char[2048];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is,
                    "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        String text = writer.toString();
        return text;
}
like image 107
Kanaiya Katarmal Avatar answered Mar 23 '26 04:03

Kanaiya Katarmal