how to check if internal storage file has any data

Posted by user3720291 on Stack Overflow See other posts from Stack Overflow or by user3720291
Published on 2014-06-09T21:22:26Z Indexed on 2014/06/09 21:24 UTC
Read the original article Hit count: 254

Filed under:
|
|
|
public class Save extends Activity {

int levels = 2;
int data_block = 1024;
//char[] data = new char[] {'0', '0'};
String blankval = "0";
String targetval = "0";
String temp;
String tempwrite;

String string = "null";
TextView tex1;
TextView tex2;


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.save);

    Intent intent = getIntent();
    Bundle b = intent.getExtras();
    tex1 = (TextView) findViewById(R.id.textView1);
    tex2 = (TextView) findViewById(R.id.textView2);

     if(b!=null)
        {
            string =(String) b.get("string");
        }
     loadprev();
     save();
}

public void save()
{
    if (string.equals("Blank")) blankval = "1";
    if (string.equals("Target")) targetval = "1";
    temp = blankval + targetval;
    try
    {
        FileOutputStream fos = openFileOutput("data.gds", MODE_PRIVATE);
            fos.write(temp.getBytes());
            fos.close();
    }
    catch (FileNotFoundException e) {e.printStackTrace();}
    catch (IOException e) {e.printStackTrace();}


    tex1.setText(blankval);
    tex2.setText(targetval);
}

public void loadprev()
{
    String final_data = "";
    try {
        FileInputStream fis = openFileInput("data.gds");
        InputStreamReader isr = new InputStreamReader(fis);
        char[] data = new char[data_block];
        int size;
        while((size = isr.read(data))>0)
        {
            String read_data = String.copyValueOf(data, 0, size);
            final_data += read_data;
            data = new char[data_block];
        }
        }
    catch (FileNotFoundException e) {e.printStackTrace();}
    catch (IOException e) {e.printStackTrace();}
    char[] tempread = final_data.toCharArray();;
    blankval = "" + tempread[0];
    targetval = "" + tempread[1];
}

}

After much tinkering i have finally managed to get my save/load function to work, but it does have an error, pretty much i got it to work then i did a fresh reintall deleting data.gds, afterwards the save/load function crashes because the data.gds file has no previous values.

can i use a if statment to check if data.gds has any values in it, if so how do i do it and if not, then what could i use instead?

© Stack Overflow or respective owner

Related posts about android

Related posts about crash