- Home /
Reading a specific line from a file.
I'm working in Javascript.
I need to read a specific portion of a file and I don't want to waste time reading the entire file to an array and then selecting the data I need.
I'm using the StreamReader Class, but it doesn't appear to have a SkipLine or Skip method... Is there another way of moving to a specific part of a file for reading?
Answer by kevork · Sep 21, 2011 at 10:56 PM
You aren't "wasting time" by reading in the previous lines first. In text files, lines are characters, generally '\n', just like all the other characters in the file. There's no way to skip ahead to the Nth line in a generic text file without reading all the previous lines.
I would suggest you for you to not worry about performance issues until they actually become a problem.
Well unfortunately I do have a problem... I'm currently reading a 32$$anonymous$$B file... quite a lot of the time as well...
Is there a good way of giving the lines 'lines numbers' and then reading the line by its number?
It should only take a few seconds to read 32$$anonymous$$B from disk. Unfortunately, you probably want to try to solve this problem in another way. Can you store this data in another format at build time, so it's immediately available at run time? Are you reading this file more than once? Etc.
What other formats might be more suitable? I'm reading this file to update a terrain as the player moves over it. (It's actually a planet but that's beside the point) As the player moves around Unity is currently accessing the file containing the planets terrain and building the area the player can see.
The planet has been split into 1000 chunks in a 3D grid. If a chunk is in view its data is loaded from the file and created in realtime. The problem is that with a large file it really slows down the game as the world is loaded because you're right, it takes a second or two to read the 32$$anonymous$$B file, but it has to do it fairly often when the player is moving.
Why don't you split the file into 1000 bite-sized chunks? That way you only have to load the bit that you need, and you still get all of your data available.
Do you mean have 1000 different files? Or split the file in a different way?
Answer by drudiverse · Mar 20, 2016 at 08:21 AM
I believe this does it, puts them all in array, its a bit shorter than parsing manually:
var arrLine : String[] = File.ReadAllLines(fileName);
arrLine[line_to_edit - 1] = newText;
File.WriteAllLines(fileName, arrLine);
Your answer
![](https://koobas.hobune.stream/wayback/20220612221637im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make/write to files, then read immediately? 1 Answer
I cant read the file i wrote, unless i reload it in VS. 1 Answer
Best way to manage stats in a text file 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
how to read a txt file faster ? my projects' loading time is so long ! about 30 minutes!!! 2 Answers