Android 无法通过java在文件中写入数据
我正在创建一个文本文件并将一些数据写入其中。我要在文件中写入的字符串 已成功写入,但每当我要在文件中写入新行(使用\n)时,它会在notpad文件中显示一些框,如[][][]。如何在文本文件中写入新行字符。告诉我我做错了什么。这是我的代码。提前谢谢Android 无法通过java在文件中写入数据,android,file,fileoutputstream,Android,File,Fileoutputstream,我正在创建一个文本文件并将一些数据写入其中。我要在文件中写入的字符串 已成功写入,但每当我要在文件中写入新行(使用\n)时,它会在notpad文件中显示一些框,如[][][]。如何在文本文件中写入新行字符。告诉我我做错了什么。这是我的代码。提前谢谢 try { //FileWriter f = new FileWriter("/sdcard/download/possible.txt"); FileOutputStream fOut = openFileOu
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);
我认为System.getProperty(“line.separator”);这将为您运行的平台获得合适的新行字符
请看这篇文章。上面的代码在一行中给出数据。我想插入\n但失败。这里的map、weightmap、netcarri、exerciseamp是链接的哈希映射。请阅读此文章。我还建议为您的报告使用XML文件:)我相应地修改了代码,但问题尚未解决。它对输出文件没有影响。请帮助尝试使用PrintWriter并调用println来写行。