Download and saving .txt from server to Android

Hi, I have run into a snag after trying to implement a retrieve and save function. I want to be able to download a .txt file from the server and then save it. But all the methods I have tried doesn’t work, even just using the downloaded file directly (Which I seem to figure out as well if it were to be my second option).

Can anyone find wrong here that may give me a clue as to why it is not working. If it is possible can someone demonstrate how to use a file(the stream reader part because you need a filepath) once downloaded with the WWW class.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

   
/*
 * newDat[0] - surname
 * newDat[1] - name
 * newDat[2] - title
 * newDat[3] - email
 * newDat[4] - birthday
 * newDat[5] - management
 * newDat[6] - subjects
 * newDat[7] - sport
 * newDat[8] - code
 * newDat[9] - guardian
 * newDat[10] - classroom
*/

public class DataSorting : MonoBehaviour 
{
	private string url = "https://www.dropbox.com/s/ijpxzvwo8o7es2a/StaffData.txt?dl=0";
	private string fileName = "StaffData.txt";
	private string filePath; 

	public static int fileLenght; //Lenght(Line count) of the file

	private string[] unsortedData;

	public static string[] newDat = new string[12];

	private StreamReader dataReader;

	public static List<string>[] teacherData; //List that stores all data on the teachers

	void ReadData()
	{
		filePath = Application.persistentDataPath + "/" + fileName;
		dataReader = new StreamReader(filePath);
		unsortedData = new string[fileLenght];
		for(int i = 0; i < fileLenght; i++)
		{
			unsortedData *= dataReader.ReadLine();*
  •  }*
    
  •  dataReader.Close();*
    
  •  teacherData = new List<string>[fileLenght];*
    
  • }*

  • void SortData()*

  • {*

  •  for(int i = 0; i < fileLenght; i++)*
    
  •  {*
    

_ newDat = unsortedData*.Split(‘,’);_
_ teacherData = new List();
teacherData.Add(newDat[0]);
teacherData.Add(newDat[1]);
teacherData.Add(newDat[2]);
teacherData.Add(newDat[3]);
teacherData.Add(newDat[4]);
teacherData.Add(newDat[5]);
teacherData.Add(newDat[6]);
teacherData.Add(newDat[7]);
teacherData.Add(newDat[8]);
teacherData.Add(newDat[9]);
teacherData.Add(newDat[10]);
}
print (teacherData[0][2]);
}*_

* IEnumerator DownloadData()*
* {*
* WWW www = new WWW(url);*
* yield return www;*

* File.WriteAllText(Application.persistentDataPath + “/” + fileName, www.text);*
* print (“doen”);*
* }*

* void Awake()*
* {*
* StartCoroutine(DownloadData());*

* fileLenght = File.ReadAllLines(filePath).Length;*

* ReadData();*
* SortData();*
* }*

* void Update()*
* {*

* }*

}

Your logic would be to first check if the file exists on the device, and if not then you download.

string file = null;
void RetrieveData(){
     if(File.Exists(path)){
            this.file = File.ReadAllText(path);
            return;
     }
     StartCoroutine(DownloadData());
}

IEnumerator DownloadData(){
       WWW www = new WWW(url);
       yield return www;
       // Check www is right
       this.file = www.text;
       File.WriteAllText(path, this.file);
}

Just to make it easier, instead of using your own file system, I would consider json or xml. You can then use parsers to read, write data.