Remove a specific line from a .txt file

Pizzret

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:

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

Everytime I call this method like this:

removeLineFrom(externalStoragePath + File.separator + 
    "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", 
    recordedFilesArray.get(toDelete));

The app crashes. This is the logcat error:

08-08 15:24:22.225: E/AndroidRuntime(6146): java.lang.IndexOutOfBoundsException: 
    Invalid index 1, size is 1

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:

toDelete = arg2;

Whole onItemLongClick if anyone needs:

listView.setOnItemLongClickListener(new OnItemLongClickListener(){

        @Override
        public boolean onItemLongClick(AdapterView<?> 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;
        }

    });
Guy

The problem is here:

listAdapter.remove(listAdapter.getItem(toDelete));
listAdapter.notifyDataSetChanged();

removeLineFrom(externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/recorded files.txt", recordedFilesArray.get(toDelete));

You are removing arg2 from the array, before you call your method, that's why when you call your method, the arg2 (toDelete) doesn't exist anymore. To make it work, simply call your method (removeLineFrom) before removing item from listAdapter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove specific line from txt file

Remove Line from String within txt file

How to remove one line from a txt file

Remove line breaks from txt file

Remove line from txt file if it endswith xyz

Remove specific white spaces from txt file

Reading a specific line from a *.txt file in VHDL

Matching and splitting a specific line from a .txt file

Reading specific line from .txt file

read specific line from txt file java

Remove line that contain specific element in a txt file python

How to remove line with specific string from file

Read txt file from specific of line to a certain line base on string

add or remove from line or character from txt file in cpp

copy line from a txt file and replace it in another txt file at specific number line using PowerShell

Read and remove first (or last) line from txt file without copying

node.js remove a line from .txt file

remove new line from txt file after insert value

How remove the last brak line from the txt file with JAVA?

How to remove hidden line breaks from TXT file in C?

How to remove specific numbers from a txt file with SED or AWK?

Remove specific value from .txt file while using (readtable) MATLAB

How to delete a specific line and the following n lines from a txt file?

R - Reading lines from a .txt-file after a specific line

Querying a specific line from a txt file in C#

Print line above specific string from txt file

Update specific line of a txt file

How to remove line matching specific pattern from a file

What is the fastest way to remove a specific line from a big file?