Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Moytas · Sep 16, 2015 at 12:11 PM · unity 5file-io

How to append a string to a file if the file exists

Hello all, I want to get data to a .txt file when the game finishes. But the file might already be created, so i want to append the new line. (I have been searching and i haven't found the right way to do it) Can you please help me?

The code i'm using is:

 string tmp = string.Format ("DATE: {0} \r\n PLAYER: {1} TIME: {2} POSITION: {3}", System.DateTime.Now, data.id, data.score, DataStorage.instance.posTxt.text);
         if (File.Exists (Application.persistentDataPath + "/" + data.id + ".dat")) {
             File.AppendAllText (Application.persistentDataPath + "/" + data.id + ".dat", tmp);
         } else {
             File.Create (Application.persistentDataPath + "/" + data.id + ".dat");
             StreamWriter sr = File.Open(Application.persistentDataPath + "/" + data.id + ".dat",FileMode.Open);
             sr.WriteLine (tmp);
             sr.Close ();
         }


Thank you all in advance

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by sys12 · Sep 16, 2015 at 12:15 PM

http://stackoverflow.com/questions/8255533/how-to-add-new-line-into-txt-file

Comment
Add comment · Show 2 · 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 Moytas · Sep 16, 2015 at 12:25 PM 0
Share

yes!! thank you very much sys12! that's what i was looking for

avatar image sys12 Moytas · Sep 16, 2015 at 12:31 PM 0
Share

No problem. Glad I can help.

avatar image
0

Answer by codemaker2015 · Oct 16, 2021 at 02:52 PM

Try the following code to create and write content to a text file in Unity3D.

 void CreateLog() {
         string timestamp = DateTime.Now.ToString("dd-mm-yyyy_hh-mm-ss");
         PlayerPrefs.SetString("timestamp", timestamp);
         string path = Application.persistentDataPath + "/" + "log_" + timestamp + ".txt";
         // This text is added only once to the file.
         if (!File.Exists(path)) {
             // Create a file to write to.
             using (StreamWriter sw = File.CreateText(path))
             {
                 sw.WriteLine(DateTime.Now.ToString() + ": " + "App initialised");
             }   
         } else {
             // This text is always added, making the file longer over time
             // if it is not deleted.
             using (StreamWriter sw = File.AppendText(path)) {
                 sw.WriteLine(DateTime.Now.ToString() + ": " + "App initialised");
             }   
         }
 
         // Open the file to read from.
         using (StreamReader sr = File.OpenText(path)) {
             string line = "";
             while ((line = sr.ReadLine()) != null)
             {
                 Debug.Log(line);
             }
         }
 }
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

Unity-Android read file from device storage 1 Answer

Can not save data to file and then load it in Unity3d 2 Answers

Why is this error happening 1 Answer

Camera problem ,Unity 5 1 Answer

how to vew other island on facebook game ?,hw to access other island on facebook game unity3d? 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