How to count lines fast?
File.ReadLines was introduced in .NET 4.0 var count = File.ReadLines(file).Count(); works in 4 seconds, the same time as the first code snippet
File.ReadLines was introduced in .NET 4.0 var count = File.ReadLines(file).Count(); works in 4 seconds, the same time as the first code snippet
Just read it first before you get into the loop. I’d do this: using (StreamReader sr = new StreamReader(filePath)) { string headerLine = sr.ReadLine(); string line; while ((line = sr.ReadLine()) != null) { … } } (I don’t like using Peek, personally.) Then when you write out the output, start with headerLine.
ReadBlock does not mean it is thread safe. If you use Reflector to look at the implementation of StreamReader.ReadBlock (which is inherited from TextReader.ReadBlock), all it does is make multiple calls to the “Read” method until either the “Read” method returns 0 or we have read as many bytes as requested. This is needed because … Read more
Yes, StreamReader.Dispose closes the underlying stream (for all public ways of creating one). However, there’s a nicer alternative: using (TextReader reader = File.OpenText(“file.txt”)) { } This has the added benefit that it opens the underlying stream with a hint to Windows that you’ll be accessing it sequentially. Here’s a test app which shows the first … Read more
No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it’s GC’d sooner): using (StreamReader r = new StreamReader(“file.txt”)) { allFileText = r.ReadToEnd(); } Or alternatively in .Net 2 you can use the new File. static members, then you don’t need to close anything: … Read more
Both StreamReader and BinaryReader can be used to get data from binary file Well, StreamReader can be used to get text data from a binary representation of text. BinaryReader can be used to get arbitrary binary data. If some of that binary data happens to be a representation of text, that’s fine – but it … Read more
The leak is here: new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) XmlSerializer uses assembly generation, and assemblies cannot be collected. It does some automatic cache/reuse for the simplest constructor scenarios (new XmlSerializer(Type), etc), but not for this scenario. Consequently, you should cache it manually: static readonly XmlSerializer mySerializer = new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) and use the cached serializer … Read more
As Jared says, You cannot do this unless the other entity which has the file open allows for shared reads. Excel allows shared reads, even for files it has open for writing. Therefore, you must open the filestream with the FileShare.ReadWrite parameter. The FileShare param is often misunderstood. It indicates what other openers of the … Read more
If you are using .NET 4.0, try MemoryMappedFile which is a designed class for this scenario. You can use StreamReader.ReadLine otherwise.
Original Answer (Read Update Below!) Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a … Read more