0

I am creating a text file and writing some data into it.the string i want to write in file is written successfully but whenever i want to write the new line (use \n) in file it shows some boxes in that notpad file like[][]][]. How can i write the new line character in text file.tell me what i m doing wrong.Here is my code.Thanks in advance.

      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); 
picaso
  • 713
  • 2
  • 14
  • 26
  • above code gives data in a single line .i want to insert \n but failed.here map,weightmap,netcalori,exerciseamp are linked hash maps. – picaso Jan 13 '12 at 11:08
  • read this mate. http://www.coderanch.com/t/276869/Streams/java/Line-break-fileOutputStream I would also recommend going with an XML file for your Report :) – Sergey Benner Jan 13 '12 at 11:27

1 Answers1

0

I think System.getProperty("line.separator"); is what your looking for, this will get the appropriate new line characters for the platform you are running on.

Have a look at this post post.

Community
  • 1
  • 1
The Ed R
  • 316
  • 2
  • 6
  • i modified my code accordingly but the problem is not solves yet.It does not have effect on output file.please help – picaso Jan 13 '12 at 11:21
  • try using PrintWriter [link](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/PrintWriter.html) and calling println to write lines. – The Ed R Jan 13 '12 at 11:45