java - Can't Delete local File - Error "Is a Directory" -
i have searched vast internet no success. writing file , trying delete it. code has commented out statements have tested writing file contents both simple string, works. however, when try make create file edittext (casted string) has error. don't understand.
someone please me issue.
when press delete button, 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 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify 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 + " deleted"); } else { log.d(tag, name + " not deleted"); } }
i think it's because file name initialized in oncreate
method, in case if edittext
doesn't have text in xml
layout definition, filename
empty.
you should invoke (string) editname.gettext().tostring()
in btndelete
click listener. or set text change listener edittext
, update filename
field every time when text changes.
Comments
Post a Comment