java - Saving a file in Android -


i have following code:

    btnsavetrip.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             // todo auto-generated method stub             if (showlog != null && showlog.gettext().tostring().length() > 0) {                 file folder = new file(environment.getexternalstoragedirectory() + "/tc");                 if (!folder.exists()) {                     folder.mkdir();                 }                 string externalstoragepath = environment.getexternalstoragedirectory().tostring();                 final file file = new file(externalstoragepath + "/tc/strip.tcl");                 try {                     if (file.exists()) {                         new alertdialog.builder(getactivity())                         .settitle("file exist")                         .setmessage("do want overwrite file?")                         .setpositivebutton("yes", new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int which) {                                 outputstream = new fileoutputstream(file);                                 outputstream.write(showlog.gettext().tostring().getbytes());                                 toast.maketext (getactivity(), file.tostring(), toast.length_short).show();                             }                         })                         .setnegativebutton("no", new dialoginterface.onclicklistener() {                             public void onclick(dialoginterface dialog, int which) {                                 // nothing                             }                         })                         .show();                     }                     else {                         outputstream = new fileoutputstream(file);                         outputstream.write(showlog.gettext().tostring().getbytes());                         toast.maketext (getactivity(), file.tostring(), toast.length_short).show();                     }                 }                 catch (ioexception e) {                     e.printstacktrace();                     toast.maketext (getactivity(), "error in try", toast.length_short).show();                 }                 {                     if(outputstream!=null) {                         try {                             outputstream.close();                             toast.maketext (getactivity(), "file closed", toast.length_short).show();                         }                         catch (ioexception e) {                             // todo auto-generated catch block                             e.printstacktrace();                             toast.maketext (getactivity(), "error in catch", toast.length_short).show();                         }                     }                 }             }             else {                 toast.maketext (getactivity(), "empty", toast.length_short).show();             }         }     }); 

what looking is:

when button clicked:

1) check make data not null or empty (working fine):

if (showlog != null && showlog.gettext().tostring().length() > 0) { 

2) check make sure folder exists, if not create folder (working fine):

            file folder = new file(environment.getexternalstoragedirectory() + "/tc");             if (!folder.exists()) {                 folder.mkdir();             } 

3) before writing data file, ensure doesn't exist. if exist, prompt user see if can overwritten. if user chooses yes overwrite file if user chooses no add "1" @ end of filename , save it. (not working , need help)

i getting error following line:

outputstream = new fileoutputstream(file); outputstream.write(showlog.gettext().tostring().getbytes()); 

error:

unhandled exception type filenotfoundexception

--> (surround try/catch)

in answer part 3), i've edited code work you. of code had correctly written there couple of issues. moved code write new file new method writefile() in order avoid replicating code , make easier keep track of what's going on:

btnsavetrip.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view v) {         // todo auto-generated method stub         if (showlog != null && showlog.gettext().tostring().length() > 0) {             file folder = new file(environment.getexternalstoragedirectory() + "/tollculator");             if (!folder.exists()) {                 folder.mkdir();             }             string externalstoragepath = environment.getexternalstoragedirectory().tostring();             final file file = new file(externalstoragepath + "/tollculator/strip.tcl");             if (file.exists()) {                 new alertdialog.builder(getactivity())                 .settitle("file exist")                 .setmessage("do want overwrite existing file?")                 .setpositivebutton("yes", new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         writefile(file);                     }                 })                 .setnegativebutton("no", new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         dialog.dismiss();                     }                 })                 .show();             } else {                 writefile(file);             }         } else {             toast.maketext (getactivity(), "empty", toast.length_short).show();         }     } });  // ...  private void writefile(file file){     try {         outputstream = new fileoutputstream(file);         outputstream.write(showlog.gettext().tostring().getbytes());         toast.maketext (getactivity(), file.tostring(), toast.length_short).show();     } catch (filenotfoundexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     } {         if(outputstream!=null) {             try {                 outputstream.close();                 toast.maketext (getactivity(), "file closed", toast.length_short).show();             } catch (ioexception e) {                 e.printstacktrace();                 toast.maketext (getactivity(), "error in catch", toast.length_short).show();             }         }     } } 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -