0

I have a .txt file which contains let's say

1;2;3;4;5
a;b;c;d;e
A;B;C;D;E

And I would like to remove the line which begins with "a" I made a copy of the file and write there the lines unless the line equals the lineToRemove

So here what's I did but the file hasn't change

String path = "playlist.txt"
String lineToRemove = "a";



public boolean removeLineFromFile(String lineToRemove) {  


try {
    File inFile = new File(path);
    //Creating a temp file
    File tempFile = new File(inFile.getAbsolutePath()+".tmp");

    FileInputStream fIn = openFileInput(path);
    InputStreamReader isr = new InputStreamReader(fIn);
    BufferedReader br = new BufferedReader(isr);                    

    FileOutputStream fOut_temp = openFileOutput(path +".tmp", Context.MODE_APPEND);
    OutputStreamWriter osw_temp = new OutputStreamWriter(fOut_temp);
    osw_temp.write("");

    String line = br.readLine();

    //Read from the original file and write to the new
    //unless content matches data to be removed.
    while (line != null) {
        String[] tokens = line.split(";");
        if (! tokens[0].equals(lineToRemove)){
            osw_temp.write(line);
            osw_temp.flush();
        }
        line = br.readLine();
     }

    osw_temp.close();
    br.close();               

    //Delete the original file   
    inFile.delete();
    //Rename the new file to the filename the original file had.
    tempFile.renameTo(inFile);  
    return true;              
}catch (Exception ex) { return false;}  

I think there is a problem with using File, is there another way of writing on android internal storage ? Thanks in advance for your help

EDIT : because using File = new File + rename + deleted methods weren't working here is the solution that I find out. Maybe not the best but at least it works

try {   

     FileInputStream fIn = openFileInput(path);
     InputStreamReader isr = new InputStreamReader(fIn);
     BufferedReader br = new BufferedReader(isr);  

     //Create temp file 
    FileOutputStream fOut2 = openFileOutput("te.txt", Context.MODE_WORLD_WRITEABLE);
    OutputStreamWriter osw2 = new OutputStreamWriter(fOut2);

    osw2.write("");
    // save and close
    osw2.flush();
    osw2.close();

    // Adding things to temp file
    FileOutputStream fOut_temp = openFileOutput("te.txt", Context.MODE_APPEND);
     OutputStreamWriter osw_temp = new OutputStreamWriter(fOut_temp);
     osw_temp.write("");

      String line = br.readLine();


      //Read from the original file and write to the new
      //unless content matches data to be removed.
      while (line != null) {
        String[] tokens = line.split(";");
        if (! tokens[0].equals(lineToRemove)){
          osw_temp.write(line);
          osw_temp.write("\r\n");
          osw_temp.flush();
        }
        line = br.readLine();
      }

      osw_temp.close();
      br.close();


      //Delete the original file  
    FileOutputStream fOut = openFileOutput(path, Context.MODE_WORLD_WRITEABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut);          
    osw.write("");
    // save and close
    osw.flush();
    osw.close();

    //Copy temp file to original file

      FileInputStream fIn3 = openFileInput("te.txt");
     InputStreamReader isr3 = new InputStreamReader(fIn3);
     BufferedReader br2 = new BufferedReader(isr3);
    String line4 = br2.readLine() ;

   FileOutputStream fOut_temp4 = openFileOutput(path, Context.MODE_APPEND);
   OutputStreamWriter osw_temp4 = new OutputStreamWriter(fOut_temp4);

      while (line4 != null) {

          osw_temp4.write(line4);
          osw_temp4.write("\r\n");
          osw_temp4.flush();
            Toast.makeText(getApplicationContext(),"ecrit", Toast.LENGTH_SHORT).show();

        line4 = br2.readLine();
      }

      osw_temp4.close();
      br2.close();

      return true;            
    }catch (Exception ex) {                 
        Toast.makeText(getApplicationContext(),ex.getMessage(), Toast.LENGTH_SHORT).show();
    return false;}        

}

Lol Pallau
  • 199
  • 2
  • 5
  • 15

1 Answers1

1

Using this with Java I'm able to remove the line starts with a, just port in Android thats it.

public class LineRemover
{
static String path = "temp.txt";
    static String lineToRemove = "a";
    public static void main(String[] args)
    {
            try {
                File inFile = new File(path);

                FileInputStream fIn = new FileInputStream(path);
                InputStreamReader isr = new InputStreamReader(fIn);
                BufferedReader br = new BufferedReader(isr);                    

                FileOutputStream fOut_temp = new FileOutputStream("te.txt");
                OutputStreamWriter osw_temp = new OutputStreamWriter(fOut_temp);
                osw_temp.write("");

                String line = br.readLine();

                //Read from the original file and write to the new
                //unless content matches data to be removed.
                while (line != null) {
                    String[] tokens = line.split(";");
                    if (! tokens[0].equals(lineToRemove)){
                        osw_temp.write(line);
                        osw_temp.flush();
                    }
                    line = br.readLine();
                 }

                osw_temp.close();
                br.close();               

                inFile.delete();
            inFile = new File("te.txt");
            //Rename the new file to the filename the original file had.
            inFile.renameTo(new File("temp.txt"));  
            }catch (Exception ex) 
            {}
        }
}
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • i tried adding File tempFile = newFile("te.txt");tempFile.renameTo(inFile); at the end of your code but it didn't work neither. What I must have at the end is the same file with the line removed – Lol Pallau Feb 02 '13 at 12:56
  • EDIT : working great on pc but I find out that on android you can't make File inFile = new File(path); it thows an exception – Lol Pallau Feb 02 '13 at 17:40