• 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
Question by isaacpeiris1 · Oct 08, 2017 at 02:09 PM · txtstreamreader

why .txt file read and write Stuck my unity android app?

I have a txt file to read by using StreamReader and write using StreamWriter on android platform. File will have to fill only about 100 lines. It works perfectly on unity editor, but it stuck on android. I checked the file by going to the android app folder and it has written the data to the file which i programmed. But the app is not work it stucks for hours. I would be glad if anyone can tell me, what am i missing. Thanks.

 string path = (Application.persistentDataPath+"/Assets/stage0.txt");
             string linea;
 
             StreamReader reader = new StreamReader(path); 
 
             linea = reader.ReadToEnd();
 
             string[] result = linea.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
 
             reader.Close();
 
             StreamWriter writer = new StreamWriter(path, true);
             if (result.Length == 0) {
                 for (int i = 0; i < questions0.myqis.Questions.Count; ++i){
                     writer.WriteLine(i);    
                 }
             }
 
             writer.Close();
 
             //AssetDatabase.ImportAsset(path); 
             //TextAsset asset = Resources.Load("Assets/stage0.txt") as TextAsset;
 
             StreamReader reader1 = new StreamReader(path); 
             linea = reader1.ReadToEnd();
 
             string[] result1 = linea.Split(Environment.NewLine.ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
 
             reader1.Close();
 
 
             if (questions0.myqis.myNiceNumbers.Count == 0) {
                 for (int i = 0; i < result1.Length; ++i){
 
                     if (questions0.myqis.myNiceNumbers.Contains (int.Parse(result1[i]))) {
 
                     } else {
                         questions0.myqis.myNiceNumbers.Add (int.Parse(result1[i]));
                     }
 
                 }
             }
         //--------------------------------------------
 
 
             randomIndex = UnityEngine.Random.Range (0, questions0.myqis.myNiceNumbers.Count);
             randomNumber = questions0.myqis.myNiceNumbers [randomIndex];
             questions0.myqis.myNiceNumbers.RemoveAt (randomIndex);
 
             string searchFor = randomNumber.ToString();
             string tempFile = Path.GetTempFileName();
 
             using(var sr = new StreamReader((Application.persistentDataPath+"/Assets/stage0.txt")))
             using(var sw = new StreamWriter(tempFile))
             {
                 string line;
 
                 while((line = sr.ReadLine()) != null)
                 {
                     if(line != searchFor)
                         sw.WriteLine(line);
                 }
             }
 
             File.Delete((Application.persistentDataPath+"/Assets/stage0.txt"));
             File.Move(tempFile, (Application.persistentDataPath+"/Assets/stage0.txt"));
 
 

Comment
Rocketman_Dan

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by Rocketman_Dan · Oct 09, 2017 at 08:24 AM

If you're just using it to store strings, floats, or ints then use PlayerPrefs. It also saves you the work of doing all that text file parsing. Otherwise use Json files to store complex structures, it's human readable and can be extracted or written with single methods rather than building a custom text parser.

Comment
isaacpeiris1

People who like this

1 Show 3 · 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 isaacpeiris1 · Oct 09, 2017 at 09:06 AM 0
Share

I have to save list data so i think PlayerPrefs won't work. Can you tell me if there is anything wrong with my script. I'm not familiar with Json files, are Json files faster than txt?

avatar image Rocketman_Dan isaacpeiris1 · Oct 09, 2017 at 11:04 AM 0
Share

I can't see anything major jumping out at me but this it'sn't very maintainable and every time you want to add different parameters to the file you have to edit this script heavily. By using JsonUtility you can just have a class with the data you need stored in it and pass it into the JsonUtility.ToJson() function. That makes the class a string and then you can just write all text to a file. Reading from it you can just read it as a string and pass the string to JsonUtility.FromJson<ClassName>() and passing that to an object of type ClassName. That way every time you need to add, remove or alter data you're saving you don't need to rewrite the parser. The outputted file looks like this:

 {
     "id": 1,
     "name": "A green door",
     "price": 12.50,
     "tags": ["home", "green"]
 }

I made the mistake of doing custom text parsing in university last year for a coursework in a software development class and the headaches it caused having over 300 lines of code just for handling persistent data were the worst. Well over 90% of the bugs I had were rooted in edge cases or oversights I'd made when building the text file data storage solution. I had an inferior end product because although people were telling me to try using Json or xml because it was cleaner and easier but I kept going because of all the time I'd put into it.

I'll admit I don't know much about android but your question reminded me of my past experiences. I hope this helped in some way.

avatar image isaacpeiris1 · Oct 09, 2017 at 11:22 AM 0
Share

Ok Thanks, I'll try to do this with Json anyway

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

72 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Im trying to read and split a txt file, then place them into a list, When I tested it, it only had added one entry to the list. Can you guys help me understand whats going wrong ? Thank you 1 Answer

Access file in Android with Streamreader 0 Answers

Read text file to get 2D array 1 Answer

Resources.load probleme 2 Answers

Help with reading a text file? 1 Answer


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