2017-07-28 24 views
-1

ファイルに非常に単純なものを保存しようとしています。しかし何らかの理由で最初の66個の呪文を保存するだけです。私は多くの異なるコードを試しましたが、実際には何も動作しません。私の現在のコード:Android保存文字列txtファイル

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(); 
     } 
    } 
+0

あなたはそれが働い – Alok

答えて

2

は以下のようにファイルに書き込むOutputStreamWriterクラスを使用してみてください:

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()); 
} 
+0

を取得しているものをエラー。ありがとうございました! –

関連する問題