如果要创建文件并多次写入文件并向其添加数据,请使用以下代码,如果不存在则将创建文件,如果存在则将添加数据。

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

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐