Given a text file, how would I go about reading an arbitrary line and nothing else in the file?
Say, I have a file test.txt. How would I go about reading line number 15 in the file?
All I've seen is stuff involving storing the entire text file as a String array and then using the value of the line number as the number of the String to use from the array... but there are some complications: The text file is enormously huge and the machine that the application I'm coding isn't exactly a top-notch system. Speed isn't the top priority, but it is definitely a major issue.
Are there any ways to ONLY read a specific line of a text file and store the result as a string?
Thanks for your responses: The file is KINDA structured. It's got 25 lines of info and then X lines of numbers but line 17 of the first 25 has the value of X.
But then, there's 1 blank line and it repeats itself all over as a second record in the file and X can have a different value for each record.
What I want to do is read and store the first 25 lines as independent values and then store the next X (usually around 250) lines as an array. Then I'm going to store it in an SQL database and repeat with the NEXT record until I reach the Yth record (the number of records in the file is in line 3)
EDIT 2: Alright, I think I've gotten to a solution based on a combination of your alls' responses.
I'm going to read the first 25 lines and store it as an array. I'll copy the pertinent contents of the array to local variables then I'll delete the first 25 lines. Then, I can use the info to store the next X lines (the value of item 13 in the array) as an array, serialize it, store it in a database then delete the lines that I just read.
I could then repeat the process for each subsequent record.
Of course, this relies on one assumption I'm making, which to be honest, I'm not sure is true. Is it possible to delete the first n lines from a text file from within C# without having to read the entire thing and re-write it without the first n lines?
Use the linecache. getline() method to read specific line from a file. Get line lineno from a file named filename . This function will not return any error if the line is not present in a file instead, it will return an empty string.
Method 1: fileobject.readlines() A file object can be created in Python and then readlines() method can be invoked on this object to read lines into a stream. This method is preferred when a single line or a range of lines from a file needs to be accessed simultaneously.
The line must be terminated by any one of a line feed ("\n") or carriage return ("\r"). In the following example, Demo. txt is read by FileReader class. The readLine() method of BufferedReader class reads file line by line, and each line appended to StringBuffer, followed by a linefeed.
Which function is used to read single line from file? content = fh. readline().
.NET 4.0 edit
Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 15:
string line = File.ReadLines(FileName).Skip(14).Take(1).First();
This will return only the line required
Since you can't predict the location (can you?) of the i-th line in the file, you'll have to read all previous lines too. If the line number is small, this can be more efficient than the ReadAllLines
method.
string GetLine(string fileName, int line) { using (var sr = new StreamReader(fileName)) { for (int i = 1; i < line; i++) sr.ReadLine(); return sr.ReadLine(); } }
If each line is a fixed length then you can open a Stream around it, seek (bytes per line) * n into the file and read your line from there.
using( Stream stream = File.Open(fileName, FileMode.Open) ) { stream.Seek(bytesPerLine * (myLine - 1), SeekOrigin.Begin); using( StreamReader reader = new StreamReader(stream) ) { string line = reader.ReadLine(); } }
Alternatively you could just use the StreamReader to read lines until you found the one you wanted. That way's slower but still an improvement over reading every single line.
using( Stream stream = File.Open(fileName, FileMode.Open) ) { using( StreamReader reader = new StreamReader(fileStream) ) { string line = null; for( int i = 0; i < myLineNumber; ++i ) { line = reader.ReadLine(); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With