制作临时文件并在Android内存中重命名

时间:2013-02-02 12:32:00

标签: android io fileinputstream fileoutputstream

我有一个.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;}        

}

1 个答案:

答案 0 :(得分:1)

将它与Java一起使用我能够删除行 a ,只是Android中的端口。

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) 
            {}
        }
}