Search code examples
javaandroidfileexistscreation

File created but returns false


I don't understand why we don't enter in the second if. I first check if the file exists (no, logic), I create it, and I check again but it still returns false. I tried an hour find the problem and I'm sure it's a stupid error. Sorry for my poor english

Here is the code:

package com.example.testcreatefileonclick;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements android.view.View.OnClickListener{

    Button button;
    Button addTeam;
    Boolean append = true;

    String name = "nomFichier.txt";

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

        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener((OnClickListener) this);
        addTeam = (Button)findViewById(R.id.button2);
        addTeam.setOnClickListener((OnClickListener) this);

    }

    @Override
    public void onClick(View v) {
        try {
            File fichier = new File(name);


            if (!fichier.exists()) {
                System.out.println("File doesn't exists");
            }


            FileOutputStream fOut = openFileOutput(name, MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut); 
            osw.write("text");
            osw.flush();
            osw.close();

            //Why don't we go in this if ?
            if (fichier.exists()) {
                System.out.println("File exists");
            }


        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

Edit: Working Code

package com.example.testcreatefileonclick;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements android.view.View.OnClickListener{

    Button button;
    Button addTeam;
    Boolean append = true;
    String name = "nomFichier.txt";

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

        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener((OnClickListener) this);
        addTeam = (Button)findViewById(R.id.button2);
        addTeam.setOnClickListener((OnClickListener) this);

    }

    @Override
    public void onClick(View v) {
        try {
            String filePath = (this.getFilesDir().getPath().toString());
            File fichier = new File(filePath + name);

            if (!fichier.exists()) {
                System.out.println("File doesn't exists");
            }

            fichier.createNewFile();
            FileWriter file = new FileWriter(filePath + name);
            file.write("text");
            file.flush();
            file.close();

            if (fichier.exists()) {
                System.out.println("File exists");
            }


        } catch (IOException e) {
            System.out.println("Exception");
            e.printStackTrace();
        }
    }
}

Solution

  • Try replacing this :

    FileOutputStream fOut = openFileOutput(name, MODE_WORLD_READABLE);
            OutputStreamWriter osw = new OutputStreamWriter(fOut); 
            osw.write("text");
            osw.flush();
            osw.close();
    

    by this :

    fichier.createNewFile();
    FileWriter file = new FileWriter(name);
    file.write("text");
    file.flush();
    file.close();