From my iPad I try to save data to a text file and read it. The string from the textfile will be displayed in text field. On my MAC everything works fine, but if I try it on my iPad I always get the following error:
DirectoryNotFoundException: Could not find a part of the path
This is the code I am using:
Text txtField
public void SaveDataToTextFile()
{
using(StreamWriter saveData = new StreamWriter(Application.dataPath + "/Resources/TextFiles/textfile.txt"))
{
saveData.Write("Some Text");
saveDate.Close();
ReadSavedData();
}
// I also tried it with Application.persistenDataPath and Application.StreamingAssets
// But with both solutions I received the error above
}
public void ReadSavedData()
{
using(StreamReader readData = new StreamReader(Application.dataPath + "Resources/TextFiles/textfile.txt"))
{
var fileContent = savedData.ReadToEnd();
var lines = fileContent.Split(new char[] { '\n' });
foreach(var line in lines)
{
var savedDataString = line.Split(new char[] { '\n' });
txtField.text = string.Join("\n", savedDataString.Skip(1));
}
}
}
I know this question is similar to this question and even to this but non of them really helped me.
What do I have to change so that my iPad can write to and read from a text file?
Edit
I could manage to read my text file with my iPad with the following code:
public void ReadSavedData()
{
// Read dueDates TextFile:
var textFile = Resources.Load<TextAsset>("TextFilePath/textFileName");
var lines = textFile.text.Split(new char[] { '\n' });
foreach(var line in lines)
{
var savedDataString = line.Split(new char[] { '\n' });
txtField.text = string.Join("\n", savedDataString.Skip(1));
}
}
But I am still not able to write into my text file.