0
我正在学习Android,我正在制作一些基本的数学程序(游戏)。它会得到两个随机数,随机算子和30秒来尽可能地解决数学问题。如果你解决问题你得到1分。读取和写入文件Android
无论如何,现在我想获得用户已经完成的点数,并将其写入文件,然后再读取它(现在只是记录它)。
当我点击按钮,写入文件,它和我得到这个日志消息: 09-21 21:11:45.424:DEBUG /写(778):这是写日志:2
呀,似乎它写道。奥基,让我们读一读。
09-21 21:11:56.134:DEBUG /读日志(778):这是读日志:2
它读取它。
但是当我再次尝试写入时,它似乎会覆盖以前的数据。 (778):这是写日志:1 09-21 21:17:28.334:DEBUG/Reading日志(778):这是读日志:1
正如你所看到的,它只是读取最后一个输入。
这是代码的一部分,我正在写和读它。
public void zapisi() {
// WRITING
String eol = System.getProperty("line.separator");
try {
FileOutputStream fOut = openFileOutput("samplefile.txt",
MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(poenibrojanje+eol);
//for(int i=0;i<10;i++){
Log.d("Writing","This is writing log: "+poenibrojanje);
//}
//osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void citaj() {
// READING
String eol = System.getProperty("line.separator");
try {
BufferedReader input = new BufferedReader(new InputStreamReader(
openFileInput("samplefile.txt")));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = input.readLine()) != null) {
buffer.append(line + eol);
}
//TextView textView = (TextView) findViewById(R.id.result);
Log.d("Reading log","This is reading log:"+buffer);
System.out.println(buffer);
//tvRezultat.setText(buffer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
你能解释一下好一点?当我试试这个:FileOutputStream中FOUT = openFileOutput(“samplefile.txt”,真正 \t \t \t \t \t MODE_WORLD_READABLE)我得到这个错误:在类型ContextWrapper的方法openFileOutput(字符串,整数)不适用的参数(字符串,boolean); – Zookey
噢,现在当我试图读取它时,ffs会出现此错误,或者写入:09-21 21:46:19.185:WARN/System.err(832):java.io.FileNotFoundException:/samplefile.txt – Zookey
你的文件路径有问题。你在哪里保存文件? – Manos