Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Reading a Text file with Stream reader on Android

Discussion in 'Scripting' started by Simpso, Mar 6, 2019.

  1. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    HI Everyone hoping you can help with a problem ive been banging my head against.

    Basically i have a tab delimited text file with a bunch of information in.
    I have saved this in the REsources folder and i am currently reading it in using Streamreader with the below code and then pushing it into a list array:

    Code (csharp):
    1.  reader = new StreamReader(Application.dataPath + "/Resources/Pokemon master list.txt");
    On pc this works fine.

    When i port over to Android its almost like it cannot see the file so consequently no info is show in the app.

    Ive searched around with no luck so far.
    Ive tried switching the file path to streamingassets, doing a resources.load and switching the file path to the android specific one with the file://jrar at the start. Still no success.

    Is anyone able to help me out with some code or tell me what i might be doing wrong for this to not work on Android devices?

    Thanks as always.

    UPDATE:

    Managed to fix my issue i switched to using resources.load
    Code (csharp):
    1.  
    2.         TextAsset theList = (TextAsset)Resources.Load("Pokemon master list", typeof (TextAsset));
    3.         string txtContent = theList.text;
    And moved away from Streamreader to stringreader.
    Solved all my problems.
     
    Last edited: Mar 6, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Streamreader relies on an underlying filesystem. Android doesn't offer you one for the APK you upload.

    Using Resources.Load() is entirely within Unity and will work every time.

    As an aside, you can write that a bit cleaner with generics:

    Code (csharp):
    1. TextAsset theList = Resources.Load<TextAsset>("Pokemon master list");
    2. string txtContent = theList.text;
     
  3. Simpso

    Simpso

    Joined:
    Apr 20, 2015
    Posts:
    158
    Thanks this isn't what was trying originally. Turns out I needed to drop the .txt off the end and switch to stringreader.
    Using what I used above is what happened when I got code desperate and couldn't work out why it wasn't working :)
    But will update back to what you suggested now.

    Thanks for the info.
     
  4. ViooGames

    ViooGames

    Joined:
    Jan 2, 2020
    Posts:
    4
    i was writing full file name with extension that is
    TextAsset theList = Resources.Load("levels/lvl" + lev + ".txt") as TextAsset;

    and it was solved once i removed .txt
    TextAsset theList = Resources.Load("levels/lvl" + lev ) as TextAsset;

    Hope it helps :)