无法删除本地文件 - 错误"是目录"

时间:2015-04-14 15:59:38

标签: java android file android-edittext

我在广阔的互联网上搜索没有成功。我正在写一个文件然后尝试删除它。我的代码有一些注释掉的语句,我已经测试过它用一个简单的字符串写一个包含内容的文件,这个工作。但是,当我尝试从EditText创建一个文件(转换为String)时,它有一个错误。我不明白。

有人请帮我解决这个问题。

当我按下删除按钮时,会出现此错误:

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");
    }

}

1 个答案:

答案 0 :(得分:0)

我认为这是因为您的文件名已在onCreate方法中初始化,因此如果您的editTextXML布局定义中没有文字,fileName将会是空的。

您应该在(String) editName.getText().toString()点击监听器中调用btnDelete。或者为文本更改时设置editText的文本更改侦听器并更新fileName字段。