xamrain 写txt文件

来源:互联网 发布:linux启动软件命令 编辑:程序博客网 时间:2024/05/17 08:48

注意:如果文件夹不存在先创建文件夹

目前只能先创建文件再写入;

创建并写入的方法还没找到

static string logFilepath =Android.OS.Environment.ExternalStorageDirectory.ToString ()
+ "/fpdata/";
static string logfile;
/**
* 保存文件
* */
public static bool saveLog(string dataname,string data) {
try {
logfile=logFilepath+dataname+".txt";
// Java.Net.URI url=new Java.Net.URI(logfile);
Java.IO.File fi=new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.ToString (),"fpdata");
if(!fi.Exists())
fi.Mkdirs ();
Java.IO.File fi2=new Java.IO.File(logFilepath,dataname+".txt");
if(!fi2.Exists())
fi2.CreateNewFile ();
FileOutputStream fos = new FileOutputStream(logfile);
byte[] d= System.Text.Encoding.ASCII.GetBytes(data);
fos.Write (d,0,d.Length);
fos.Flush ();
fos.Close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
return false;
}
}


0 0
原创粉丝点击