mardi 30 juin 2015

Java/Android: Byte to file not saving?

I'm wondering what's wrong with this code:

public class MainActivity extends ActionBarActivity {
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.textview);
        start();
    }
 public void start(){
        File file = new File("file.file");   
        try{

            FileOutputStream fos;
            fos = openFileOutput("file.file", Context.MODE_PRIVATE);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            if(file.length() == 0){
                BigInteger bi = new BigInteger("1000");
                bos.write(bi.toByteArray());
                bos.flush();
                bos.close();
                textView.setText(""+file.length()); //This is always 0, why? 
            }
            else{
             //do stuff, but since file.length() is always 0, this never happens. 
            }


}

So basically I want to check if the file is empty, if it's empty then add stuff to it. So what I expect is that when I open the application next time, the length shouldn't be empty since I've written to it earlier. However, the length of the file is always 0, why is that? File.length() says it returns the amount of bytes.

Aucun commentaire:

Enregistrer un commentaire