Android:如何写入特定行,Java
大家好谢谢
我想知道如何使用安卓在java中编写特定行。以下是我的写作方法:
public void saveLesson(int lessons2, int problem2, int mlessons2, int mproblem2, String checkAccuracy) {
String newline = System.getProperty("line.separator");
final String saved = new String(lessons2 + newline + problem2 + newline + mlessons2 + newline + mproblem2 + newline + checkAccuracy);
FileOutputStream fOut = null;
try { // catches IOException below
fOut = openFileOutput("lessonStore.properties",
Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
try {
osw.write(saved);
/* ensure that everything is
* really written out and close */
osw.flush();
osw.close();
} catch (IOException e) {
Toast andEggs = Toast.makeText(Lessons.this, "Error while writing...", Toast.LENGTH_SHORT);
andEggs.show();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我如何让它只覆盖第3行,而不是整个内容,因为这是为了保存变量,最好只保存一个而不是所有变量
# 1 楼答案
你没有
永远不要在一行中修改文件;如果你的线路比原来的短怎么办?如果更长呢?操作系统不会自动为您扩展/收缩文件
将内容写入另一个文件,然后替换原来的文件