テキストファイルの作成と編集に問題があります。ファイルにデータが保存されているようには見えません。
- テキストファイルが利用できない場合は、作成する必要があります。
- ファイルにデータがある場合は、そのデータを読み取って利用できるようにします。
- 保存されるデータは、
String
次のように区切られた3つの整数値で構成されます,
。String finalWrite = "3,5,1"
- したがって、この文字列を分割し、新しいカウンターを追加できるように整数に変換する必要があります。
- これらの新しいカウンターは、デバイスに保存されているテキストファイルに書き込む必要があります
エラーは発生せず、強制終了もありません。
Logcatを使用して、値が正しく保存されていないことを確認することしかできませんでした。
Android開発サイトのドキュメントを確認しました。誰かが私を助けたり、正しい方向に向けたりすることができれば、それは大いにありがたいです!
私が使用しているwriteメソッド:
public void WriteItIn() throws IOException
{
FileOutputStream fOut = openFileOutput("stats.txt", Context.MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
ReadItIn(); //calls the read method, to get the values from the file
int tmp1 = 0 + countertmp + counter;
int tmp2 = 0 + counterpostmp + counterpos;
int tmp3 = 0 + counternegtmp + counterneg;
finalwrite = "" + tmp1 + "," + tmp2 + "," + tmp3;
osw.write(finalwrite);
osw.flush();
osw.close();
}
Readメソッド:
public void ReadItIn() throws IOException
{
FileInputStream fIn = openFileInput("stats.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[fIn.available()];
isr.read(inputBuffer);
stringFromFile = new String(inputBuffer);
String [] tmp = stringFromFile.split("\\,");
if(tmp.length > 0)
{
Log.d("READ", " NOT NULL");
for(int i = 0;i<tmp.length ; i++)
{
String temper = tmp[i];
if(temper == null || temper == "")
{
Log.d("NULL", "NULLIFIED");
}
else
try
{
int x = Integer.parseInt(temper, 10);
if(i == 0){counter = x;}
else if(i == 1){counterpos = x;}
else if(i == 2){counterneg = x;}
}
catch(NumberFormatException e)
{
e.printStackTrace();
}
}
}
else
Log.d("READ", "NULL");
}