- Home /
Streamreader read .txt file with Android
I need to read some lines from .txt file and after ceratain string("co_wypisac") return the remaining part of the line as a string. It works perfectly while running on unity, but after building and exporting to android device the data from file is not visible (no display box). I do not want to use XML for few reasons. Anyone got an idea?
stream filetoread="file.txt";
StreamReader odczytaj = new StreamReader(Application.dataPath + "/Language_files/" + filetoread);
string odczyt;
while((odczyt = odczytaj.ReadLine())!= null)
{
if(odczyt.Contains(co_wypisac))
{
odczyt = odczyt.Substring(odczyt.IndexOf(' ') + 1);
odczyt= odczyt.Replace("\\n",System.Environment.NewLine)
odczytaj.Close();
return odczyt;
}
}
odczytaj.Close();
Answer by flamy · Nov 08, 2013 at 07:20 PM
The reason is simple the target file is not included in the build since the file didn't have any linked reference with the scene. So wat you have to do is make unity force include the file.
note that files inside Resources folder is always included in build.
so all u have to do is make a folder called Resources under assets folder and move the language folder into it.
so now you hav to,pass path as Application.datapath+"Resources/Language_files/"+filetoread
Edit sorry for the blunder I have made,
2 points to be mentioned, In android, the file you have kept in any of the project folder cannot be accessed seperately. Because of this, You wont be able to use streamreader. Files in resource folder can be accessed and loaded at runtime. but not using stream reader. you have to load it as a text asset and get the whole content of file loaded.
one thing to note is that the file should be of .txt extension for loading it using TextAsset.
//the file name is testfile.txt so im using here as testfile
TextAsset odczytaj = Resources.Load("Language_files/testfile") as TextAsset;
string[] linesFromfile = odczytaj.text.Split("\n"[0]);
foreach (string odczyt in linesFromfile)
{
Debug.Log(odczyt);
if(odczyt.Contains(co_wypisac))
{
//dosomething
}
}
The code of yours would change a bit and resemble like this.
sorry for the mistake, check the answer now i have edited it
GREAT IT WOR$$anonymous$$ED!! THAN$$anonymous$$S $$anonymous$$AN!
mark the answer as accepted, if the problem is solved :) :)
I use this for JSON, strea$$anonymous$$g doesn't work but your solution works ! thank you i upvote you
The Strea$$anonymous$$gAssets
folder is also included in the build, but the files are not loaded into Unity. It is a good option to use with StreamReader
, but it does not work with Resources.Load
Your answer
Follow this Question
Related Questions
Alpha problem with textures on a plane 2 Answers
Unity 2018 UGUI Android Sprite Textures Not Rendering Correctly 1 Answer
Android textures not loading 1 Answer
DXT texture format on Mali GPU 0 Answers