Question

I've been working on a personal app and Stack Overflow has helped a bit so far, but I've now run into another issue. I'm attempting to read a basic text file stored in my source code and output it to an alert dialog. My code does this, but the dialog does not display any of my new lines.

displayChangelogDialog method

private void displayChangelogDialog() {
    Context context = this;
    AssetManager am = context.getAssets();
    InputStream is;
    // ensure that changelog is available
    try {
        is = am.open("changelog");
        // changelog dialog
        new AlertDialog.Builder(this)
                .setTitle("Changelog")
                .setMessage(getStringFromInputStream(is)) // convert changelog to string
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // do nothing
                    }
                })
                .show();
    } catch (IOException e) {
        Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

getStringFromInputStream method

private static String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();

}

changelog text file

v0.0.3
- Update PPS rate for recent difficulty increase

v0.0.2
- Calculate DGM based on PPS rate

I have attempted to add "\n" to the end of each line, but it does not work and the characters "\n" are simply displayed. Thanks in advance everyone.

Was it helpful?

Solution

There is an easy and hack way to read all of the inputstream into a string object which contains all you need without read line by line.

Scanner scanner = new Scanner(inputStream).useDelimiter("\\A");
String string = scanner.hasNext() ? scanner.next() : null;
scanner.close();

OTHER TIPS

readLine() will read up to a linefeed, but not include the linefeed. Also, there is no reason to use a string builder here. Change to this:

String result = "";

String line;
try {

    br = new BufferedReader(new InputStreamReader(is));
    while ((line = br.readLine()) != null) {
        result += line + "\n";
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

return result;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top