0

我只想从我创建的文本文件中删除 2 行文本。目前,这就是我试图摆脱的 2 行:

private void deleteDataFromFile(String title, String Date) {

        try {
            //open the file
            FileInputStream myIn= openFileInput(FILENAME);
            //the reader given the input file stream.
            InputStreamReader inputReader = new InputStreamReader(myIn);
            //Aftert he inputstream is open, need also a bufferedReader
            BufferedReader BR = new BufferedReader(inputReader);

            //holds a line of input
            String line;

            //used for only erasing one date.
            int counter = 0;

            while ((line = BR.readLine()) != null && counter < 1) {
                if (line.equals(title)) {
                    line.replace(title, "" + "\n");
                    line = BR.readLine();
                    line.replace(Date, "" + "\n");
                    counter++;
                }
            }
            BR.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


}

我意识到,由于我在字符串上使用替换函数,它对实际文件本身没有影响,但我在 IO 库中找不到任何其他函数来影响文本文件。我正在考虑做的是在手机上使用该文件的确切内容创建一个新文件,然后从中删除两行。不过,这似乎很麻烦且效率低下,我想避免它。有什么建议么?

4

0 回答 0