androideclipseinputstreamfile-ioasset-management

Android, reading in multiline text file groups the text together, need assistance


I have a working filereader for a text file in my Raw Dir. The user should see the text file in the same format as I have formatted it in word but when the application is played, the text file is tightly grouped together with no paragraphs.

This is the file reader method below, as I said it does work but I just want the format to be as I have made it.

Your advice and guidance will be greatly appricated

@Override
protected void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.activity_support);

    InputStreamReader isr = new InputStreamReader(this.getResources().openRawResource(R.raw.supporttext));
    BufferedReader br = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    String s;

    try{
        while ((s = br.readLine()) != null){
            sb.append(s);
        }
    }catch (IOException e) {
        e.printStackTrace();
    }
    TextView tv = (TextView)findViewById(R.id.supporttxt);
    tv.setText(sb.toString());

Solution

  • From BufferedReader#readline java doc :

    A String containing the contents of the line, not including any line-termination characters

    So using readline strips the line-termination characters, that's why you do not see them in your Text View. You can add them back like this :

    try{
        while ((s = br.readLine()) != null){
            sb.append(s);
            sb.append("\n");
        }
    }catch (IOException e) {
        e.printStackTrace();
    }
    

    Also, you should probably use getText instead of openRawResource