2

I have searched the vast Internet with no success. I am writing a file and then trying to delete it. My code has some commented out statements where I have tested it writing a file with contents both from a simple string, this works. However, when I try to make it create a file from an EditText (casted to a String) it has an error. I don't understand.

Someone please help me with this issue.

When I press the delete button, this error comes up:

04-14 11:40:38.086  24584-24584/com.test.dev.write_delete_local_file E/Exception﹕ File write failed: java.io.FileNotFoundException: /data/data/com.test.dev.write_delete_local_file/files: open failed: EISDIR (Is a directory)

MainActivity.java:

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

    editName = (EditText)findViewById(R.id.nameEdit);
    editContent = (EditText)findViewById(R.id.contentEdit);
    btnWrite = (Button)findViewById(R.id.btnWrite);
    btnDelete = (Button)findViewById(R.id.btnDelete);

    fileContents = (String) editContent.getText().toString();
    fileName = (String) editName.getText().toString();

    btnWrite.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

            writeFile(fileName, fileContents);
        }

    });

    btnDelete.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            deleteTheFile(fileName);
        }

    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void writeFile(String name, String data) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput(name, Context.MODE_PRIVATE));
        //OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("test.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        //outputStreamWriter.write("abcdefghijklmnop");
        outputStreamWriter.close();

        Toast.makeText(this.getBaseContext(), (CharSequence) this.getFilesDir().toString(), Toast.LENGTH_SHORT).show();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    }
}

public void deleteTheFile(String name){
    File chosenFile = context.getFileStreamPath(name);
    //File chosenFile = this.getFileStreamPath("test.txt");
    boolean fileDeleted = chosenFile.delete();
    if (fileDeleted) {
        Log.d(TAG, name + " was deleted");
    } else {
        Log.d(TAG, name + " was not deleted");
    }

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pyroclasm
  • 31
  • 3
  • This looks similar to this post. http://stackoverflow.com/questions/3775694/deleting-folder-from-java – thethereit Apr 14 '15 at 16:07
  • I looked through that post and used the "deleteDirectory()" function suggested, however is there anyway I can check for sure that the file was deleted? – Pyroclasm Apr 14 '15 at 19:48

1 Answers1

0

I think it's because your file name is initialized in onCreate method, so in case if your editText doesn't have text in XML layout definition, fileName will be empty.

You should invoke (String) editName.getText().toString() in btnDelete click listener. Or set text change listener for editText and update fileName field every time when text changes.

pbespechnyi
  • 2,251
  • 1
  • 19
  • 29
  • I've added that and it seems to work, but not sure if it works. I noticed that I need a way to verify whether the file was created or not. Thus I've added a read file to my code, but it doesn't seem to work. :S – Pyroclasm Apr 16 '15 at 15:53
  • @Pyroclasm, if you've written something to file, then it was created successfully. – pbespechnyi Apr 16 '15 at 15:55