Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Ivalaostia · Dec 05, 2014 at 08:40 PM · androidfilestreamingassetsfilestreamstreamreader

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;
         }
     }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Dec 05, 2014 at 04:52 PM 0
Share

do you have the correct path for android?

avatar image Ivalaostia · Dec 05, 2014 at 05:59 PM 1
Share

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.

avatar image Lunatix · Dec 06, 2014 at 11:29 AM 0
Share

Is the file included in the build? And which exception gets thrown?

avatar image Ivalaostia · Dec 06, 2014 at 01:10 PM 0
Share

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.

avatar image moghes · May 13, 2015 at 08:23 AM 0
Share

@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 :)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
 
     }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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:

http://blogs.unity3d.com/2009/04/15/bug-reporting-and-you/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

StreamingAssets GIF images on android 0 Answers

File paths on Android 0 Answers

File.WriteAllText() on Android (Json file) 2 Answers

Reading from a file not working on Android 0 Answers

Get Contents of Folder Using UnityWebRequest 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges