无法获取 Android 以将数据附加到 .txt 文件

Can't Get Android to Append Data to .txt file

我在这里很新,并且一直在关注我能找到的所有关于让我的程序在计时器用完时将一系列数字(由按下按钮的频率确定)附加到 .txt 文件的方法,但无济于事——我不确定我错过了什么……该应用程序首先在下载文件夹中创建一个包含模板的 .txt 文件:

String initialTemplate ="\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\",\"12\"";
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS);
    File file = new File(path, filename+".txt");
    try {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(initialTemplate.getBytes());
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

然后当倒数计时器对象完成并 restarts/ends 时,它会触发 class writeresults() 附加值:

if (CountsRemaining < 1){
                textViewTime.setText("00:00:00");
                cancel();
                    writeResults();
                finishcount();
            } else {
                //append data
                    writeResults();
                //Clear all variables
                clearCounts();
                start();
            }

重要的部分是实际写入数据,这是行不通的:

public void writeResults(){
String Message = Count1 + ", " + Count2 + ", " + Count3 + ", " + Count4 + ", " + Count5 + ", " +
        Count6 + ", " + Count7 + ", " + Count8 + ", " + Count9 + ", " + Count10 + ", " + Count11 + Count12;
FileWriter writer;
try {
    File path = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOWNLOADS);
    File file = new File (path + filename+".txt");
    writer = new FileWriter(file, true);
        writer.append(Message);
        writer.append("this is a writing test message");
        writer.close();
        Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

知道我可能遗漏了什么吗?参数?

问题出在这一行:

File file = new File (path + filename+".txt");

应该是这样的,如果你看你的第一个代码,在创建文件时:

 File file = new File (path, filename+".txt");