- Home /
Can't read lines from file by using StreamReader on Android platform
I need to read a text stream by using StreamReader from file on android platform. File is about 100k lines, so even editor is getting stuck if i try to load it all to TextAsset or if i use WWW.
I simply need to read that file line by line without loading it all to a string. Then i'll do a tree generation from the lines that i got from the file. (But probably that part doesn't matter, i just need help on file reading part.)
I'm giving the code that i wrote down below. It works perfectly on editor, but fails on android.
I would be glad if anyone tell me, what am i missing.
(ps. english is not my native and this is my first question on the site. so sorry for the any mistakes that i may have done.)
private bool Load(string fileName)
{
try
{
string line;
string path = Application.streamingAssetsPath +"/";
StreamReader theReader = new StreamReader(path + fileName +".txt", Encoding.UTF8);
using (theReader)
{
{
line = theReader.ReadLine();
linesRead++;
if (line != null)
{
tree.AddWord(line);
}
}
while (line != null);
theReader.Close();
return true;
}
}
catch (IOException e)
{
Debug.Log("{0}\n" + e.Message);
exception = e.Message;
return false;
}
}
File is in the Strea$$anonymous$$gAssets folder. So Application.strea$$anonymous$$gAssetsPath +"/" should be giving me the right path if i'm not wrong.
For being sure I was displaying the folder address. Which is:
jar:file///mnt/asec/[[PAC$$anonymous$$AGE_NA$$anonymous$$E]]-2/pkg.apk!/assets
It should be correct i think.
Is the file included in the build? And which exception gets thrown?
Yes the file is in the build at the exact folder I wrote above (I checked it via root browser on my phone, so it is there)
Also I am displaying that exception message to GUI, but unfortunately no exceptions are thrown.
@Ivalaostia its been past months to your post and I guess you had found a solution. Please answer your own post in case you have a valid solution to this problem. Thanks :)
Answer by Ivalaostia · May 13, 2015 at 12:57 PM
Sorry for forgetting to post the way I've resolved this issue. I've simply copied the file from streaming assets folder to android persistent datapath. That way I became able to read it line by line instead of loading it all once to the memory.
Here's the code:
private IEnumerator Load4(string fileName)
{
// Handle any problems that might arise when reading the text
try
{
datapathh = Application.dataPath+"/assets/";
TextAsset txtAsset = (TextAsset)Resources.Load (fileName, typeof(TextAsset));
string txtContent = txtAsset.text;
System.IO.File.WriteAllText(Application.persistentDataPath+"/" + fileName + ".txt" , txtContent,Encoding.UTF8);
string line;
StreamReader theReader = new StreamReader(Application.persistentDataPath+"/" + fileName + ".txt" , Encoding.UTF8);
GlobalData.Instance.letterCounts = new Dictionary<char, int>();
using (theReader)
{
// While there's lines left in the text file, do this:
do
{
line = theReader.ReadLine();
linesRead++;
if (line != null)
{
tree.AddWord(line);
}
}
while (line != null);
theReader.Close();
}
}
catch (IOException e)
{
Debug.Log("{0}\n" + e.Message);
}
yield return true;
}
Answer by unimechanic · Dec 09, 2014 at 04:21 PM
Have you tried with a small file? If that works, please submit a bug report for reading the large file:
Your answer
Follow this Question
Related Questions
StreamingAssets GIF images on android 0 Answers
File paths on Android 0 Answers
File.WriteAllText() on Android (Json file) 2 Answers