0

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.

optional
  • 273
  • 1
  • 5
  • 13
  • @AndyThomas: Correct, deleted my comment. – kosa Jun 30 '15 at 22:32
  • 1
    @optional - Check whether your variable `file` is referring to the same actual path as the file you've opened in the method `openFileOutput()`. (Better yet, pass the variable `file` into `openFileOutput()`, to eliminate the question.) – Andy Thomas Jun 30 '15 at 22:42
  • I just checked this now but unfortunately the problem is still there. :( – optional Jun 30 '15 at 22:54

2 Answers2

2

You need to check for emptiness before opening the file. Opening an output file creates a new zero length file, unless you specify an 'append' mode.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

I had a similar problem when first exploring Android development. Try creating your files like this:

File f = new File(context.getFilesDir(), "file.file");

Where context is your app's current android.content.Context.

Edit:

File f = new File(this.getFilesDir(), "file.file");

...because ActionBarActivity extends Activity, which is a context. I haven't developed for Android in a while, but apparently ActionBarActivity is now deprecated. Look into that post for more information.

Community
  • 1
  • 1
  • Hm well this problem had a funny solution. This is really just part 1 of the solution. There were 3 steps: Step 1) Follow EJP's answer. Step 2) Implement ePluribusFunk's answer. Step 3) Move start() to the activity's onStart-method. It was in the onCreate-method earlier, which meant I had to quit the application to get the if/else-statement to run again. Just minimizing the application by pressing for example "Home", will not trigger onCreate() when the app is opened up the next time – optional Jul 01 '15 at 09:52