• 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 Albathroz · Nov 08, 2017 at 02:36 PM · jsonpersistentdatapathiostreamreader

Problems when saving and loading with FileStream

Hello everybody. I'm having some File Sharing problems in unity. I need to save and load objects from a few Json files and even though I used StreamReader.Close(), it doesn't seem to work as it should. I'm a bit slow with those backend code stuff, so I really don't know what I'm doing wrong. Does anyone have any suggestion?

 using UnityEngine;
 using System.Collections;
 using System.IO;
 using System.Text;
 
 public class SaveLoadManager : MonoBehaviour {
         public SaveData saveData;
 
 
     public static SaveLoadManager manager;
 
     void Awake(){
         if (manager == null) {
             manager = this;
         } else if (manager != null) {
             Destroy (gameObject);
         }
         DontDestroyOnLoad (gameObject);
     }
         
 
     public static string LoadJsonAsResource(string path){
         string jsonFilePath = path.Replace (".json", "");
         TextAsset loadedJsonfile = Resources.Load<TextAsset> (jsonFilePath);
 
         return loadedJsonfile.text;
     }
 
 
     public string LoadJsonAsExternalResource(string path){
         path = Application.persistentDataPath + "/" + path;
         if (!File.Exists (path)) {
             return null;
         }
 
         StreamReader reader = new StreamReader (path);
         string response = "";
         while (!reader.EndOfStream) {
             response += reader.ReadLine();
         }
         return response;
         reader.Close ();
     }
 
     public void WriteJsonToExternalResource(string path, string content){
         
         path = Application.persistentDataPath + "/" + path; //caminho do arquivo
 
         FileStream stream = File.Create (path);//cria o arquivo
 
         byte[] contentBytes = new UTF8Encoding (true).GetBytes (content);//converting the string into bytes
         stream.Write(contentBytes,0,contentBytes.Length);//escreve os dados no arquivo
 
         stream.Close ();
     }
 
 
 }
 
Comment

People who like this

0 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
Best Answer

Answer by Paul-Sinnett · Nov 08, 2017 at 04:45 PM

Your line reader.Close() is below a return statement. It won't get executed.

Comment
Albathroz

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 Albathroz · Nov 08, 2017 at 05:08 PM 0
Share

Damn, I didn't see that, thank you! Would you have any suggestion on how to stop the Sharing Violation error when I call the WriteJsonToExternalResource? It returns that error every time I need to overwrite the text inside the JSON.

avatar image Paul-Sinnett Albathroz · Nov 08, 2017 at 05:12 PM 0
Share

If you have the file left open from an earlier run you may need to close down and restart to clear it.

avatar image Albathroz · Nov 08, 2017 at 05:58 PM 0
Share

That was it! Thank you very much, man! I changed the code and it worked like a charm. Thank you again.

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

74 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 avatar image avatar image

Related Questions

About Android can't load json file in Application.persistentDataPath 2 Answers

Android: Trying to reset save data on device when pushing a new build! 1 Answer

How to refer to internal json file on Android? 1 Answer

System.IO in javascript 1 Answer

How to send an Email with the details entered in a json file? 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