我只想从我创建的文本文件中删除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();
}
}
我意识到因为我在字符串上使用replace函数它对实际文件本身没有影响,但我在IO库中找不到任何其他函数来影响文本文件。我正在考虑做的是在手机上创建一个新文件,其中包含该文件的确切内容,并从中删除2行。这看起来很麻烦而且效率低下,我想避免它。有什么建议吗?