Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way to make sure a line exists in a plain text file

I'm using C# (.Net 2.0), and I have a fairly large text file (~1600 lines on average) that I need to check periodically to make sure a certain line of text is there.

What is the most efficient way of doing this? Do I really have to load the entire file into memory each time?

Is there a file-content-search api of some sort that I could use?

Thanks for any help/advice.

like image 594
Andrew Ensley Avatar asked May 05 '09 16:05

Andrew Ensley


People also ask

Which method is used to read all the lines one by one in a file in C#?

Use ReadAllLines() method to read all the lines one by one in a file.

How can you tell if a file is plain text?

You can call the shell command file -i ${filename} from Java, and check the output to see if it contains something like charset=binary . If it does, then it is binary file. Otherwise it is text based file.

How do you check whether a string is present in a file in Java?

The contains() method checks whether a string contains a sequence of characters. Returns true if the characters exist and false if not.

How to read a single line from a file in Python?

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.


1 Answers

Well, you can always use the FileSystemWatcher to give you an event when the file has changed, that way you only scan the file on demand.

like image 91
Brian Genisio Avatar answered Oct 12 '22 00:10

Brian Genisio