java - Remove a specific line from a .txt file -


i'm trying remove specific line .txt file, stored on android phone. how i'm trying it:

public void removelinefrom (string filepath, string linetoremove){     try {           file oldfile = new file(filepath);           file tempfile = new file(externalstoragepath + file.separator + "/android/data/com.whizzappseasyvoicenotepad/temp file.txt");            bufferedreader br = new bufferedreader(new filereader(filepath));           printwriter pw = new printwriter(new filewriter(tempfile));            string line = null;            //read original file , write new           //unless content matches data removed.           while ((line = br.readline()) != null) {              if (!line.equals(linetoremove)) {                pw.write(line);               pw.flush();             }           }           pw.close();           br.close();            //delete original file           oldfile.delete();            //rename new file filename original file had.           tempfile.renameto(recordedfiles);          }         catch (filenotfoundexception ex) {           ex.printstacktrace();         }         catch (ioexception ex) {           ex.printstacktrace();         } } 

everytime call method this:

removelinefrom(externalstoragepath + file.separator +      "/android/data/com.whizzappseasyvoicenotepad/recorded files.txt",      recordedfilesarray.get(todelete)); 

the app crashes. logcat error:

08-08 15:24:22.225: e/androidruntime(6146): java.lang.indexoutofboundsexception:      invalid index 1, size 1 

it says problem in line posted above (calling method). don't know why index invalid. todelete variable:

todelete = arg2; 

whole onitemlongclick if needs:

listview.setonitemlongclicklistener(new onitemlongclicklistener(){          @override         public boolean onitemlongclick(adapterview<?> arg0, view arg1,                 int arg2, long arg3) {             deletealert.settitle("warning");             deletealert.setmessage("are sure want delete this?");             todelete = arg2;             deletealert.setpositivebutton("yes", new dialoginterface.onclicklistener() {                  @override                 public void onclick(dialoginterface dialog, int which) {                     file directory = new file (externalstoragepath + file.separator + "android/data/com.whizzappseasyvoicenotepad/");                     file deletefile = new file (directory, recordedfilesarray.get(todelete) + ".mp3");                     deletefile.delete();                     log.i("tag", "deleting file: " + directory + recordedfilesarray.get(todelete) + ".mp3");                      listadapter.remove(listadapter.getitem(todelete));                     listadapter.notifydatasetchanged();                      removelinefrom(externalstoragepath + file.separator + "/android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedfilesarray.get(todelete));                      toast toast = toast.maketext(getapplicationcontext(), "deleted", toast.length_short);                     toast.show();                      dialog.dismiss();                 }             });              deletealert.setnegativebutton("no", null);             deletealert.show();             return false;         }      }); 

the problem here:

listadapter.remove(listadapter.getitem(todelete)); listadapter.notifydatasetchanged();  removelinefrom(externalstoragepath + file.separator + "/android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedfilesarray.get(todelete)); 

you removing arg2 array, before call method, that's why when call method, arg2 (todelete) doesn't exist anymore. make work, call method (removelinefrom) before removing item listadapter.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -