Issue
Am trying to create a new text file in Xamarin Android and write some strings to that file but am getting an error that
ENOENT(No such file or directory)
My code is explained below in detail
//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();
}
When I check in the Application's directories, I see Documents Directory but it's empty Any suggestions that will succeed to create that text file and write to it will surely be appreciated
Solution
After going through my code and some posts on this forum, i discovered that you need to delimit the string file name with a back slash so that the compiler knows how to combine the directory and text file logic....
//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();
}
It worked and when i can succesfully find the text file in Storage
Answered By - user14187680
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.