android 导入txt数据格式,如何在Android上创建文本文件并将数据插入该文件
如果要创建文件并多次写入文件并向其添加数据,请使用以下代码,如果不存在则将创建文件,如果存在则将添加数据。SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");Date now = new Date();String fileName = formatter.format(now) + ".txt";//like 2016_
如果要创建文件并多次写入文件并向其添加数据,请使用以下代码,如果不存在则将创建文件,如果存在则将添加数据。
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
Date now = new Date();
String fileName = formatter.format(now) + ".txt";//like 2016_01_12.txt
try
{
File root = new File(Environment.getExternalStorageDirectory()+File.separator+"Music_Folder", "Report Files");
//File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists())
{
root.mkdirs();
}
File gpxfile = new File(root, fileName);
FileWriter writer = new FileWriter(gpxfile,true);
writer.append(sBody+"\n\n");
writer.flush();
writer.close();
Toast.makeText(this, "Data has been written to Report File", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
e.printStackTrace();
}
更多推荐
所有评论(0)