Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove a specific line from a .txt file
    primarykey
    data
    text
    <p>I'm trying to remove a specific line from .txt file, that is stored on my android phone. This is how I'm trying to do it:</p> <pre><code>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 from the original file and write to the new //unless content matches data to be removed. while ((line = br.readLine()) != null) { if (!line.equals(lineToRemove)) { pw.write(line); pw.flush(); } } pw.close(); br.close(); //Delete the original file oldFile.delete(); //Rename the new file to the filename the original file had. tempFile.renameTo(recordedFiles); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } </code></pre> <p>Everytime I call this method like this:</p> <pre><code>removeLineFrom(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedFilesArray.get(toDelete)); </code></pre> <p>The app crashes. This is the logcat error:</p> <pre><code>08-08 15:24:22.225: E/AndroidRuntime(6146): java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 </code></pre> <p>It says the problem is in the line posted above (calling the method). I don't know why the index would be invalid. This is toDelete variable:</p> <pre><code>toDelete = arg2; </code></pre> <p><strong>Whole onItemLongClick if anyone needs:</strong></p> <pre><code>listView.setOnItemLongClickListener(new OnItemLongClickListener(){ @Override public boolean onItemLongClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { deleteAlert.setTitle("Warning"); deleteAlert.setMessage("Are you sure you want to 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; } }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload