Android- 在 txt 文件中保存字符串

Android- saving string in txt file

我正在尝试做一件非常简单的事情——将字符串保存在一个文件中。但出于某种原因,它只保存了前 66 个字符。我尝试了许多不同的代码,但实际上没有任何效果。我当前的代码:

 final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    File dir = new File(dirPath);
    if(!dir.exists())
        dir.mkdirs();
    File file = new File(dirPath, "file.txt");
    FileOutputStream stream = null;
    try {
        stream = new FileOutputStream(file);

        stream.write(myString);
        stream.flush();
        stream.close();

    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        try {
            if (stream != null) {
                stream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

尝试使用 OutputStreamWriter class 写入文件,例如:

try
{
    file.createNewFile();
    FileOutputStream fOut = new FileOutputStream(file);
    OutputStreamWriter outWriter = new OutputStreamWriter(fOut);
    outWriter.append(data);

    outWriter.close();

    fOut.flush();
    fOut.close();
}
catch (IOException e)
{
    Log.e("Exception", "File write failed: " + e.toString());
}