- Home /
How to create and access txt file in android
Hi, I am making an android app in C++ where I want to create some achievments so I need permanently changing variables and I decided to use a txt file. While I am in Unity everything works fine however when I open it with a phone nothing happens. What did I do wrong?
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.IO;
public class Test : MonoBehaviour { public bool[] ach = new bool[12]; public string high; string one;
void Start()
{
string curFile = Application.dataPath + "/achievment.txt";
if (File.Exists(curFile))
{
StreamReader be = new StreamReader(Application.dataPath + "/achievment.txt");
high = be.ReadLine();
for (int i = 0; i < ach.Length; i++)
{
one = be.ReadLine();
if (one == "True")
{
ach[i] = true;
}
else
{
ach[i] = false;
}
}
be.Close();
}
else
{
for (int i = 0; i < ach.Length; i++)
{
ach[i] = false;
}
StreamWriter ki = new StreamWriter(Application.dataPath + "/achievment.txt");
{
high = "0";
ki.WriteLine(high);
for (int i = 0; i < ach.Length; i++)
{
ki.WriteLine(ach[i]);
}
ki.Close();
}
}
}
}
Answer by Bodhid · Dec 02, 2019 at 04:45 PM
On Android it is not possible to write to "Application.dataPath", since it is not a folder.
Try changing the location of the file to "Application.persistentDataPath", which directs to something like "Android/Data/com.companyname.productname/Files/". https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612215538im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity reduces the number of available OpenMP threads 0 Answers
Save File for Android Game.. 2 Answers
low-level OpenGL code running on iOS and Android ? 0 Answers
Android Native Plugin (*.SO) 2 Answers
Recompiling my DLL as SO. 0 Answers