0

我有一个 .txt 文件,其中包含让我们说

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

我想删除以“a”开头的行我复制了文件并在那里写下这些行,除非该行等于 lineToRemove

所以在这里我做了什么但文件没有改变

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

我认为使用 File 存在问题,是否有另一种在 android 内部存储上写入的方式?在此先感谢您的帮助

编辑:因为使用 File = new File + rename + deleted 方法在这里不起作用是我找到的解决方案。也许不是最好的,但至少它有效

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

}

4

1 回答 1

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) 
            {}
        }
}
于 2013-02-02T12:45:24.037 回答