【问题标题】:There is no such file or directory error in Xamarin AndroidXamarin Android 中没有这样的文件或目录错误
【发布时间】: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


【解决方案1】:

你为什么不使用System.IO?您将能够以这种方式编写可以在 Android 和 iOS 之间共享的代码,并且仍然可以从共享代码编写文件。您唯一需要抽象的部分是从 Android 资产中读取文件。

考虑以下代码:

using System;
using System.IO;

var myDocuments = System.Environment.GetFolderPath(
    System.Environment.SpecialFolder.MyDocuments);

var myFile = Path.Combine(myDocuments, "myFiles", "SaveMe.txt");
var fileDirectory = Path.GetDirectoryName(myFile);
if (!Directory.Exists(fileDirectory))
    Directory.CreateDirectory(fileDirectory);

using var assetsStream = Assets.Open("source.txt");
using var fileStream = File.OpenWrite(myFile);
assetsStream.CopyTo(fileStream);

就是这样! using 关键字确保东西被刷新和处理。 您也可以考虑使用CopyToAsync 来代替不做阻塞IO。

其中一些语言功能可能需要您使用LangVersion 7.3 或更高版本,您可以在第一个<PropertyGroup> 中将<LangVersion>8.0</LangVersion> 添加到您的csproj 文件中

请注意,Android 上的某些路径,您需要请求权限才能从该路径写入/读取。请务必阅读有关请求运行时权限的信息。

【讨论】:

  • ,目前正在为Android平台创建应用程序
  • 嘿,我有一个文档链接,我可以用它来编程软输入键盘的 ime 操作键,以使光标在编辑文本断行中?
【解决方案2】:

在浏览了我的代码和该论坛上的一些帖子后,我发现您需要用反斜杠分隔字符串文件名,以便编译器知道如何组合目录和文本文件逻辑....

//Access the root directory of Android Os
 string abso = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
//Create New Folder in root called Tutorial
 Java.IO.File newfile = new Java.IO.File(file1, filename);
//Check if folder exists and create one if false
 if (!file1.Exists())
            {
                file1.Mkdir();
            }
//Read from the stream and assign value to string variable
   try
            {
                StreamReader streamReader = new StreamReader(assets.Open("source.txt"));
                
                    content = streamReader.ReadLine();
                      all = streamReader.ReadToEnd();
                
            } catch (System.IO.IOException e)
            {
                Toast.MakeText(this, e.Message + " ", ToastLength.Long).Show();

            }
  //Define file name with folder delimiter character "/"
   string filename = "/target.txt";
   //Combine the dir and the file to create a java file
  Java.IO.File newfile = new Java.IO.File(file1, filename);
   //Check if file exists and create new one if false
     if (!newfile.Exists())
            {
                try
                {
                    newfile.CreateNewFile();
                }catch(Java.IO.IOException k)
                {
                    Toast.MakeText(this, k.Message + " ", ToastLength.Long).Show();
                }
            }
   //Try to write to the file now  
            try
            {
                FileWriter fileWriter = new FileWriter(newfile);
                fileWriter.Write(content);
                fileWriter.Append(all);
                fileWriter.Flush();
                fileWriter.Close();
            }
            catch (System.IO.IOException j)
            {
                View view = this.FindViewById<RelativeLayout>(Resource.Id.listener);
                Snackbar.Make(view, j.Message, Snackbar.LengthLong).Show();
            }

它起作用了,当我可以成功地在存储中找到文本文件时

【讨论】:

  • / 不是转义字符。它是一个文件夹分隔符。
  • @Cheesebaron,是的,它对我有用,我很高兴我的程序可以从编辑文本中捕获文本并将字符串写入 android 操作系统中的某个文件,谢谢
猜你喜欢
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-31
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多