How to efficiently write a large text file in C#?

File I/O operations are generally well optimized in modern operating systems. You shouldn’t try to assemble the entire string for the file in memory … just write it out piece by piece. The FileStream will take care of buffering and other performance considerations. You can make this change easily by moving: using (StreamWriter outfile = … Read more

Write to a file from multiple threads asynchronously c#

For those who prefer code, I am using following to do remote logging from web apps… public static class LoggingExtensions { static ReaderWriterLock locker = new ReaderWriterLock(); public static void WriteDebug(this string text) { try { locker.AcquireWriterLock(int.MaxValue); //You might wanna change timeout value System.IO.File.AppendAllLines(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace(“file:\\”, “”), “debug.txt”), new[] { text }); } finally { locker.ReleaseWriterLock(); } … Read more

FileInputStream vs FileReader

Yes, your conclusion is correct subclasses of Reader and Writer are for reading/writing text content. InputStream / OutputStream are for binary content. If you take a look at the documentation: Reader – Abstract class for reading character streams InputStream – Abstract class is the superclass of all classes representing an input stream of bytes.

Should an object write itself out to a file, or should another object act on it to perform I/O?

The correct approach, in general, is your Case 1. This maintains a single responsibility for the class (whatever it does) without coupling it to a specific persistence mechanism (a disk). You’re looking at a specific case of a more generalized problem: Serialization. It’s good and OK for an object to have some means to indicate … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)