0

Okay, so I'm working on an my first android app. Forgive me if this sounds naive as I am a bit new at this but I'm using the following code to read from 6 different .txt files into an

private void displayFiles(int path) {
    TextView text = (TextView) findViewById(R.id.text);
    ImageView display = (ImageView) findViewById(R.id.head_image);
    String line = "";
    StringBuilder finalString = new StringBuilder();
    InputStream iStream = null;

    if (path == 0) {
        iStream = getResources().openRawResource(R.raw.salm);
        display.setImageResource(R.drawable.salm);
    } else if (path == 1) {
        iStream = getResources().openRawResource(R.raw.lyme);
        display.setImageResource(R.drawable.lyme);
    } else if (path == 2) {
        iStream = getResources().openRawResource(R.raw.pox);
        display.setImageResource(R.drawable.pox);
    } else if (path == 3) {
        iStream = getResources().openRawResource(R.raw.shig);
        display.setImageResource(R.drawable.shig);
    } else if (path == 4) {
        iStream = getResources().openRawResource(R.raw.gia);
        display.setImageResource(R.drawable.gia);
    } else if (path == 5) {
        iStream = getResources().openRawResource(R.raw.intro);
        display.setImageResource(R.drawable.intro);
    } else {
        Toast.makeText(this, "Something went wrong! Please refresh EpiQuick.", Toast.LENGTH_SHORT).show();
        return;
    }

    BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
    try {
        while ((line = bReader.readLine()) != null) {
            finalString.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    text.setText(finalString);
}

The corresponding xml:

<TextView
        android:id="@+id/text"
        style="@style/body"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/head_image"
        android:layout_marginBottom="16dp"
        />


  <style name="body">
    <item name="android:fontFamily">sans-serif-light</item>
    <item name="android:textSize">14sp</item>
    <item name="android:textColor">#000000</item>
</style>

And a sample of the text is as follows:

Those at Risk: Travelers to countries where giardiasis is common People in childcare settings Those who are in close contact with someone who has the disease People who swallow contaminated drinking water Backpackers or campers who drink untreated water from lakes or rivers People who have contact with animals who have the disease Men who have sex with men

However when I run the app after calling all of the new line characters disappear. I was wondeirng if there is a way to fix this? If so how?

Jrawr
  • 199
  • 2
  • 3
  • 15

2 Answers2

0

BufferedReader's readLine method skips the line-termination characters (docs). Since you're just displaying it to a TextView, you'll just have to manually append the new line character:

    BufferedReader bReader = new BufferedReader(new InputStreamReader(iStream));
    try {
        while ((line = bReader.readLine()) != null) {
            finalString.append(line);
            finalString.append("\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

Note: not sure if it's in some part of your code, but make sure to close bReader after use.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Where would I close bReader? It stays in use so long as the app is running. – Jrawr Sep 30 '15 at 01:07
  • This did help other wise though. Thank you. – Jrawr Sep 30 '15 at 01:20
  • You could a `finally` block to your try-catch, then call bReader.close(). – Gino Mempin Sep 30 '15 at 13:35
  • Im not to working with File I/O. Should that finally call go after the catch block or inside of it? When I try to run it outside of the catch block I get this error: undhandled exception – Jrawr Oct 01 '15 at 20:58
  • @Jrawr You also need to handle that exception, like [this](http://stackoverflow.com/a/12199273/2745495). – Gino Mempin Oct 01 '15 at 23:43
  • I'm sorry, I'm still kind of confused. I clicked the link and looked at it. Do I need to change IOException to Exception? Do I need to add a second catch to find that Exception? I haven't really had the chance to work with I/O in Java so I'm more loss in syntax than concepts. – Jrawr Oct 03 '15 at 00:18
0

I think readLine() is not including the new line character at the end of the read line so you have to add it explicitly.

    while ((line = bReader.readLine()) != null) {
        finalString.append(line);
        finalString.append('\n');
    }