2012-01-13 25 views
0

テキストファイルを作成し、それにデータを書き込んでいます。 ファイルに書き込む文字列は正常に書き込まれますが、新しい行を書きたいときはいつでも\ n )ファイルでは、[] []] []のようなnotpadファイルのいくつかのボックスを表示します。どのように私はテキストファイルに新しい行の文字を書くことができます。私は間違っていることを教えてください。これは私のコードです。事前にありがとうございます。Javaでファイルにデータを書き込むことができません

 try { 
     //FileWriter f = new FileWriter("/sdcard/download/possible.txt"); 
    FileOutputStream fOut = openFileOutput("report.txt",MODE_WORLD_READABLE); 
     OutputStreamWriter osw = new OutputStreamWriter(fOut); 
     osw.write("Diet Report"+"\n"); 
     osw.append("\nFood Calories Consumed \n"); 
     osw.append(""+SummaryReportActivity.map.keySet()); 
    osw.append(""+SummaryReportActivity.map.values()); 
     osw.append("\n"); 
    osw.append("\nExercise Calories Burnt \n"); 
    osw.append(""+SummaryReportActivity.exercisemap.keySet()); 
    osw.append(""+SummaryReportActivity.exercisemap.values()); 
      osw.append("\n"); 
      osw.append("\nWeight Recorded \n"); 
     osw.append(""+SummaryReportActivity.weightmap.keySet()); 
          osw.append(""+SummaryReportActivity.weightmap.values()); 
     osw.append("\n"); 
     osw.append("\nNet Calories Daily \n"); 
          osw.append(""+SummaryReportActivity.netCalorie.keySet()); 
          osw.append(""+SummaryReportActivity.netCalorie.values()); 
     osw.flush(); 
     osw.close(); 
     FileInputStream fIn = openFileInput("report.txt"); 
      InputStreamReader isr = new InputStreamReader(fIn); 
      char[] inputBuffer = new char[100]; 
      isr.read(inputBuffer); 
      readString = new String(inputBuffer); 

     } catch (IOException e) { 
       // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } 


//code to mail that text file as an attachement 
    Intent i=new Intent(Intent.ACTION_SEND); 
         //i.putExtra(Intent.EXTRA_EMAIL,emailaddress.getText().toString()); 
     i.putExtra(Intent.EXTRA_SUBJECT, "Summary report of your diet"); 
     i.putExtra(Intent.EXTRA_STREAM, Uri.parse("report.txt")); 
     i.setType("text/plain"); 
      startActivity(i); 
+0

上記のコードは1行にデータを与えます.iは\ nを挿入しますが、失敗しました。マップ、weightmap、netcalori、exerciseampはリンクされたハッシュマップです。 – picaso

+0

このメイトを読んでください。 http://www.coderanch.com/t/276869/Streams/java/Line-break-fileOutputStreamまた、あなたのレポートのためのXMLファイルに行くことをお勧めします:) –

答えて

0

私はSystem.getProperty( "line.separator");あなたが探しているものです。これは、実行しているプラ​​ットフォームの適切な改行文字を取得します。

postをご覧ください。

+0

私はそれに応じて私のコードを変更しましたが、問題はまだ解決しません。出力には影響しません。file.please help – picaso

+0

PrintWriter [リンク](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/PrintWriter.html)を使用してみてください。行を書き込むためにprintlnします。 –

関連する問題