【发布时间】:2020-10-26 13:20:58
【问题描述】:
我正在尝试在 Xamarin Android 中创建一个新的文本文件并将一些字符串写入该文件,但出现了一个错误
ENOENT(没有这样的文件或目录)
下面详细解释我的代码
//Create a new file where we will write some texts or strings called "SaveMe.txt"
Java.IO.File root = new Java.IO.File(Android.OS.Environment.DirectoryDocuments, "SaveMe.txt");
//Check if file does not exist and create one
if(!root.Exists()){
root.Mkdir();
}
//Define the content of the source file that we desire to access
string content;
//Read some texts from an asset file located in the Assets folder called "source.txt"
try{
AssetManager assets=this.Assets;
//Define Steam reader that we will use to read the source file
using (StreamReader streamReadr = new StreamReader(assets.Open("source.txt"))){
content = streamReadr.ReadLine();
}
//Define new File writer class that we will use to edit the target file with
FileWriter writer = new FileWriter(root);
//Add string from source file to target file
writer.Append(content);
writer.Flush();
//Close file writer
writer.Close();
}catch(Java.IO.IOException m){
//Capture the exception for debugging
Toast.MakeText(this,m.Message,ToastLength.Long).Show();
}
当我签入应用程序的目录时,我看到了文档目录,但它是空的 任何能够成功创建该文本文件并写入该文件的建议都将不胜感激
【问题讨论】:
-
请告诉您文件的完整路径。使用 Android 10 设备?
-
root.Mkdir();检查返回值并进行相应处理。 -
为什么要混合 Java.IO 和 System.IO?哪一行导致异常?
-
When i check in the Application's directories请具体说明您要如何检查。 -
资产与应用程序包/包中包含的所有项目一样,都是只读的
标签: android file xamarin writing