无法通过java将数据写入文件

发布于 2024-12-26 22:50:01 字数 2113 浏览 5 评论 0原文

我正在创建一个文本文件并向其中写入一些数据。我想在文件中写入的字符串 已成功写入,但每当我想在文件中写入新行(使用 \n)时,它会在该 notpad 文件中显示一些框,如 [][]][]。我如何在文本文件中写入新行字符。告诉我我做错了什么。这是我的代码。提前致谢。

      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); 

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); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慵挽 2025-01-02 22:50:01

我认为 System.getProperty("line.separator");是您正在寻找的内容,这将为您正在运行的平台获取适当的换行符。

看看这篇文章 发布

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文