- Home /
why .txt file read and write Stuck my unity android app?
I have a txt file to read by using StreamReader and write using StreamWriter on android platform. File will have to fill only about 100 lines. It works perfectly on unity editor, but it stuck on android. I checked the file by going to the android app folder and it has written the data to the file which i programmed. But the app is not work it stucks for hours. I would be glad if anyone can tell me, what am i missing. Thanks.
string path = (Application.persistentDataPath+"/Assets/stage0.txt");
string linea;
StreamReader reader = new StreamReader(path);
linea = reader.ReadToEnd();
string[] result = linea.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
reader.Close();
StreamWriter writer = new StreamWriter(path, true);
if (result.Length == 0) {
for (int i = 0; i < questions0.myqis.Questions.Count; ++i){
writer.WriteLine(i);
}
}
writer.Close();
//AssetDatabase.ImportAsset(path);
//TextAsset asset = Resources.Load("Assets/stage0.txt") as TextAsset;
StreamReader reader1 = new StreamReader(path);
linea = reader1.ReadToEnd();
string[] result1 = linea.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
reader1.Close();
if (questions0.myqis.myNiceNumbers.Count == 0) {
for (int i = 0; i < result1.Length; ++i){
if (questions0.myqis.myNiceNumbers.Contains (int.Parse(result1[i]))) {
} else {
questions0.myqis.myNiceNumbers.Add (int.Parse(result1[i]));
}
}
}
//--------------------------------------------
randomIndex = UnityEngine.Random.Range (0, questions0.myqis.myNiceNumbers.Count);
randomNumber = questions0.myqis.myNiceNumbers [randomIndex];
questions0.myqis.myNiceNumbers.RemoveAt (randomIndex);
string searchFor = randomNumber.ToString();
string tempFile = Path.GetTempFileName();
using(var sr = new StreamReader((Application.persistentDataPath+"/Assets/stage0.txt")))
using(var sw = new StreamWriter(tempFile))
{
string line;
while((line = sr.ReadLine()) != null)
{
if(line != searchFor)
sw.WriteLine(line);
}
}
File.Delete((Application.persistentDataPath+"/Assets/stage0.txt"));
File.Move(tempFile, (Application.persistentDataPath+"/Assets/stage0.txt"));
Answer by Rocketman_Dan · Oct 09, 2017 at 08:24 AM
If you're just using it to store strings, floats, or ints then use PlayerPrefs. It also saves you the work of doing all that text file parsing. Otherwise use Json files to store complex structures, it's human readable and can be extracted or written with single methods rather than building a custom text parser.
I have to save list data so i think PlayerPrefs won't work. Can you tell me if there is anything wrong with my script. I'm not familiar with Json files, are Json files faster than txt?
I can't see anything major jumping out at me but this it'sn't very maintainable and every time you want to add different parameters to the file you have to edit this script heavily. By using JsonUtility you can just have a class with the data you need stored in it and pass it into the JsonUtility.ToJson()
function. That makes the class a string and then you can just write all text to a file. Reading from it you can just read it as a string and pass the string to JsonUtility.FromJson<ClassName>()
and passing that to an object of type ClassName. That way every time you need to add, remove or alter data you're saving you don't need to rewrite the parser. The outputted file looks like this:
{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
}
I made the mistake of doing custom text parsing in university last year for a coursework in a software development class and the headaches it caused having over 300 lines of code just for handling persistent data were the worst. Well over 90% of the bugs I had were rooted in edge cases or oversights I'd made when building the text file data storage solution. I had an inferior end product because although people were telling me to try using Json or xml because it was cleaner and easier but I kept going because of all the time I'd put into it.
I'll admit I don't know much about android but your question re$$anonymous$$ded me of my past experiences. I hope this helped in some way.
Your answer
Follow this Question
Related Questions
Access file in Android with Streamreader 0 Answers
I'm using Java Script with import System.IO. 1 Answer
Loading TextAsset under different scenes 1 Answer
Save/Copy a .txt file a runtime 1 Answer