gpt4 book ai didi

java - Android:如何写入某一行,Java

转载 作者:行者123 更新时间:2023-12-01 13:10:39 25 4
gpt4 key购买 nike

大家好,谢谢,

我想知道如何使用 android 写入 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 行,而不是整个事情,因为这是为了保存变量,最好只保存一个而不是全部。

最佳答案

How would I make it just overwrite line 3

你不!

切勿在线修改文件;如果你的线比原来的短怎么办?如果更长呢?操作系统不会自动为您扩展/缩小文件!

将内容写入另一个文件,然后替换原始文件。

关于java - Android:如何写入某一行,Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22894964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com