2

i'm building my first android app i'm collecting 3 simple fields of data and then i will email them to a user.

i have the app up and running i can collect and display the data on the screen and i can also send the data via email. but the data is just being put on one line of text with know spaces.

i'm using FileOutputStream to write the data and i've added some text into the stream to breakup the data..but this doesnt seem to be the best route? what should i be doing? below is a sample with 2 of my fields and a static string of text.

it comes out like Balls: 22 i would like it to have spaces or headers. i guess i could add fos.write(" ".getBytes()); to make a space?

thanks

  FileOutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE);

                fos.write("Balls: ".getBytes());
                fos.write(balls.getBytes());
                fos.write(strikes.getBytes());
                fos.close(); 
branedge
  • 115
  • 3
  • 14
  • `FileOutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE)`. That should be `OutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE)`. – greenapps Mar 20 '17 at 20:59

1 Answers1

1

Why not just construct the String(s) first and then call getBytes() on the resulting String and pass it to fos.write()?

Example:

FileOutputStream fos = v.getContext().openFileOutput(file_name, MODE_PRIVATE);
String bigstring = "Balls: " + balls + " Strikes: " + strikes;
            fos.write(bigstring.getBytes());
            fos.close();

If you want to insert a newline into the String you can do it by concatenating a new-line character into it.

Community
  • 1
  • 1
Carsten Hagemann
  • 957
  • 10
  • 23
  • that worked great! thanks. i'm really new at this so wasn't aware of that. – branedge Mar 20 '17 at 20:20
  • to add the newline i tryied this but didn't work String bigstring = "Balls: " + balls + " Strikes: " + strikes %n; – branedge Mar 20 '17 at 20:36
  • You have to use %n in some actualy string, outside of it, it has no actual meaning. Therefore, `String twolines = "first line" + "%n" + "second line";` – Carsten Hagemann Mar 20 '17 at 20:45
  • thanks i couldn't get this to work but everything else worked great "Pitcher: " + p1 + "%n" + "Total: " + total + " Strikes: " + strikes + " Balls: " + balls; – branedge Mar 21 '17 at 00:49