• 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
0
Question by zero3growlithe · Jun 23, 2012 at 03:52 PM · textfilesystemread

How to read particular line from *.txt file?

How can I get a string which is for example in 4th line of a text file? Here's the code I'm using to read/write/edit file:

 import System.IO;
 var filePath = "/Users/testWrite.txt";
 
 function Update(){
     if (Input.GetKeyDown("c")) {
         WriteFile(filePath);
     }
     if (Input.GetKeyDown("w")) {
         AddToFile(filePath);
     }
     if (Input.GetKeyDown("r")) {
         ReadFile(filePath);
     }
 }
 function WriteFile(filepathIncludingFileName : String){
     var sw : StreamWriter = new StreamWriter("testFile.txt");
     sw.WriteLine("Line to write");
     sw.WriteLine("Another Line");
     sw.Flush();
     sw.Close();
 }
 function AddToFile(filepathIncludingFileName : String){
     sr = File.AppendText("testFile.txt");
     sr.WriteLine("Added ");
     sr.Write("Another Line");
     sr.Flush();
     sr.Close();
 }
 function ReadFile(filepathIncludingFileName : String) {
     sr = new File.OpenText("testFile.txt");
     input = "";
     while (true) {
         input = sr.ReadLine();
         if (input == null) { break; }
         Debug.Log("line="+input);
     }
     sr.Close();
 }
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

1 Reply

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

Answer by whydoidoit · Jun 23, 2012 at 03:59 PM

You could read the whole file into a list and then you could access any line you like: import System.Collections.Generic; import System.Linq;

   var fileLines : List.<String>;

   function ReadFile() {
      var sr = File.OpenText("whatever.txt");
      fileLines = sr.ReadToEnd().Split("\n"[0]).ToList();
      sr.Close();
   }

Now you have a list of lines that you can insert into, get a specific line from - whatever. If you want to write them back out you can just do:

  var wholeFileText = String.Join("\n", fileLines.ToArray());
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 VKronners · Jan 03, 2017 at 04:51 AM 0
Share

Can you make a C# answer also plz... I'm stuck in the same problem but im using C#...

avatar image Ahndrakhul VKronners · Jan 03, 2017 at 07:05 AM 0
Share

The easiest way to read lines of text into a list is probably:

 List<string> fileLines = new List<string>(System.IO.File.ReadAllLines("FILEPATH"));

To write them back (overwriting the old file contents):

 System.IO.File.WriteAllLines("FILEPATH", fileLines.ToArray());

"FILEPATH" would be replaced with whatever the path to your file is.

This is the easiest way as far as I know, but it's not the best performance wise. There's a lot of converting from array to list and back.

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

7 People are following this question.

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

Related Questions

Best way to manage stats in a text file 2 Answers

I cant read the file i wrote, unless i reload it in VS. 1 Answer

Move a Camera with a Data from a Text file after Build. 1 Answer

Optimize the reading of a text file? 1 Answer

How can I read data from a text file, putting a large amount of data into structures 2 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