Suika
Suika

Reputation: 660

Missing text file line breaks when opened in an Android app

I have a simple program using ScrollView and TextView that displays a long text file, but it seems to delete the line breaks in it. Example, if the text file actually has

Bunnies are mean.
But it's okay.
Because they're fluffy.

it is displayed as

Bunnies are mean. But it's okay. Because they're fluffy.

This is what my code looks like:

txt = (TextView)findViewById(R.id.txt);
stream = getResources().openRawResource(R.drawable.textBunny);
DataInputStream d = new DataInputStream(stream);
String line=null;

StringBuffer buffer=new StringBuffer();

    try {
        while((line=d.readLine())!=null){
        buffer.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    txt.setText(buffer.toString());

Help, anyone? Thanks in advance!

Upvotes: 0

Views: 125

Answers (1)

Alexander
Alexander

Reputation: 48272

buffer.append(line).append(System.getProperty("line.separator"));

Upvotes: 1

Related Questions