Android read/write file how do I set permissions?

Hello Android devs.
I have researched the generic question “how to write a file on android” and not found an answer. (in one post there is a reference to a plugin, which no longer exists)

I am trying to write a file on Android. It is an XML file and I am using the mono file I/O
I have verified I can read and write just fine on PC standalone.
but on Android I think the default android manifest does not have file read/write permissions. I did find one posting regarding the android manafest (Android Plugin and permissions - Questions & Answers - Unity Discussions) but the reply didn’t have enough detail to be useful.

Below I have included the code I use to open the file for writing, and the error I get from the android adb(adb logcat -s Unity)

Here is the code I use to open/create the file

void Start () { 
		// Where we want to save and load to and from 
             string assetsPath = Application.dataPath;
            _FileLocation = assetsPath.Substring(0,assetsPath.LastIndexOf('/')); 
	        _FileName="MuzicubeRiff.xml"; 
	} 
	
	public void OpenSaveFileForWrite()
	{
		FileInfo t = new FileInfo(_FileLocation+"\\"+ _FileName); 
		if(!t.Exists) 
		{ 
		 _writer = t.CreateText(); 
		} 
		else 
		{ 
		 t.Delete(); 
		 _writer = t.CreateText(); 
		} 
	}

and here is the adb logcat output

I/Unity   (11522): UnauthorizedAccessException: Access to the path "/data/app/com.SmoothCurvesInteractive.MuziCube-2.apk\MuzicubeRiff.xml" is denied.

I/Unity   (11522):   at System.IO.FileStream..ctor (System.String path, FileMode  mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0

I/Unity   (11522):   at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0

I/Unity   (11522):   at System.IO.FileInfo.Open (FileMode mode, FileAccess access, FileShare share) [0x00000] in <filename unknown>:0

I/Unity   (11522):   at System.IO.FileInfo.Open (FileMode mode, FileAccess access) [0x00000] in <filename unknown>:0

I/Unity   (11522):   at System.IO.FileInfo.CreateText () [0x00000] in <filename unknown>:0

I/Unity   (11522):   at (wrapper remoting-invoke-with-check) System.IO.FileInfo: CreateText ()

I/Unity   (11522):   at MuzicubeXML.OpenSaveFileForWrite () [0x00000] in <filena me unknown>:0

I/Unity   (11522):   at SaveButton.ToggleMe () [0x00000] in <filename unknown>:0

I/Unity   (11522):   at SaveButton.OnCustomTouch (.TouchItem aTItem) [0x0D/Unity   (11522): onPause

Asked and answered
Check out Preview Labs Blog File I/O in Unity3D - PreviewLabs
the key was the comment from Erique on http://forum.unity3d.com/threads/72819-Help-with-access-write-files-on-Android

Bottom Line here is how you do it:

	// Where we want to save and load to and from 
	_FileLocation=Application.persistentDataPath; 
	_FileName="MuzicubeRiff.xml"; 
		FileInfo t = new FileInfo(_FileLocation+"/"+ _FileName); 
	if(!t.Exists) 
	{ 
	 _writer = t.CreateText(); 
	} 
	else 
	{ 
	 t.Delete(); 
	 _writer = t.CreateText(); 
	} 
}

then you can

_writer.Write(“some stuff”);

and to read the file

public void OpenSaveFileForRead()
	{
		Debug.Log("Reading File " + _FileLocation+"/"+ _FileName);
		FileInfo t = new FileInfo(_FileLocation+"/"+ _FileName); 
		if(t.Exists) 
		{ 
			_reader = t.OpenText(); 
		} 
		else 
		{ 
			Debug.Log("SaveFile NotFound" + _FileLocation+"/"+ _FileName);
		} 
	}

reading is a bit more of a pain in the ass… but if you are using XML you can doit like this

 XmlSerializer xdsg = new XmlSerializer(typeof(YOUROWNSAVE));
    (YOUROWNSAVE)xdsg.Deserialize(_reader); 

Again, PreviewLabs had the best info. ,Asked and answered
Check out Preview Labs Blog File I/O in Unity3D - PreviewLabs
the key was the comment from Erique on http://forum.unity3d.com/threads/72819-Help-with-access-write-files-on-Android

Bottom Line here is how you do it:

	// Where we want to save and load to and from 
	_FileLocation=Application.persistentDataPath; 
	_FileName="MuzicubeRiff.xml"; 
		FileInfo t = new FileInfo(_FileLocation+"/"+ _FileName); 
	if(!t.Exists) 
	{ 
	 _writer = t.CreateText(); 
	} 
	else 
	{ 
	 t.Delete(); 
	 _writer = t.CreateText(); 
	} 
}

then you can _writer.Write(“some stuff”);
and to read the file
public void OpenSaveFileForRead()
{
Debug.Log(“Reading File " + _FileLocation+”/“+ _FileName);
FileInfo t = new FileInfo(_FileLocation+”/“+ _FileName);
if(t.Exists)
{
_reader = t.OpenText();
}
else
{
Debug.Log(“SaveFile NotFound” + _FileLocation+”/"+ _FileName);
}
}
reading is a bit more of a pain in the ass… but if you are using XML you can doit like this

XmlSerializer xdsg = new XmlSerializer(typeof(YOUROWNSAVE));
(YOUROWNSAVE)xdsg.Deserialize(_reader);