Hi, I am having trouble implementing FileOutputStream to save an array of data. In this code, I implement a loop which counts to 9, stores the counter values in a byte array, then writes the array to a text file on an Android device (4.2.2). The code runs and I am able to save a text file on the Android external storage and open it up on the device to view the contents. However I cannot get it to save as numerical numbers. With the code below, I am seeing two symbols of "?" which are inside of a diamond. Every other way I tried it would return a blank document. I seem to be missing something fundamental about how the "write" function of FileOutputStream saves data. If someone can help me with this data handing I would appreciate it, thank you.
package com.example.sdcardsavetest; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.concurrent.TimeUnit; import com.example.sdcardsavetest.R; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.app.Activity; import android.os.Bundle; import android.os.Environment; public class MainActivity extends Activity { File myExternalFile; private String filename = "MySampleFile.txt"; private String filepath = "MyFileStorage"; TextView TextView; byte[] array = new byte[100]; byte x = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView = (TextView)findViewById(R.id.TextView); Button startButton = (Button)findViewById(R.id.button); Button saveToExternalStorage = (Button) findViewById(R.id.saveExternalStorage); if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { saveToExternalStorage.setEnabled(false); } else { myExternalFile = new File(getExternalFilesDir(filepath), filename); } saveToExternalStorage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try{ FileOutputStream fos = new FileOutputStream(myExternalFile); System.out.println(array);//debugging fos.write(array); fos.close(); } catch (IOException ex) { } } }); startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { loop(); } }); } private static boolean isExternalStorageReadOnly() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { return true; } return false; } private static boolean isExternalStorageAvailable() { String extStorageState = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { return true; } return false; } void loop(){ runOnUiThread(new Thread(new Runnable(){ public void run(){ for (byte i = 1; i < 10; i++){ array [x] = i; TextView.setText(String.valueOf(i)); x++; try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException ie) {} } } })); } }