How do I generate a stream from a string?

public static Stream GenerateStreamFromString(string s) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; } Don’t forget to use Using: using (var stream = GenerateStreamFromString(“a,b \n c,d”)) { // … Do stuff to stream } About the StreamWriter not being disposed. StreamWriter is just a wrapper … Read more

Reading large text files with streams in C#

You can improve read speed by using a BufferedStream, like this: using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (BufferedStream bs = new BufferedStream(fs)) using (StreamReader sr = new StreamReader(bs)) { string line; while ((line = sr.ReadLine()) != null) { } } March 2013 UPDATE I recently wrote code for reading and processing (searching … Read more

Download File to server from URL

Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter: file_put_contents(“Tmpfile.zip”, fopen(“http://someurl/file.zip”, ‘r’)); From the manual: If data [that is the second argument] is a stream resource, the remaining buffer of that stream will be copied to the specified file. This is similar with using stream_copy_to_stream(). (Thanks Hakre.)

How do I save a stream to a file in C#?

As highlighted by Tilendor in Jon Skeet’s answer, streams have a CopyTo method since .NET 4. var fileStream = File.Create(“C:\\Path\\To\\File”); myOtherObject.InputStream.Seek(0, SeekOrigin.Begin); myOtherObject.InputStream.CopyTo(fileStream); fileStream.Close(); Or with the using syntax: using (var fileStream = File.Create(“C:\\Path\\To\\File”)) { myOtherObject.InputStream.Seek(0, SeekOrigin.Begin); myOtherObject.InputStream.CopyTo(fileStream); }

Java Process with Input/Output Stream

Firstly, I would recommend replacing the line Process process = Runtime.getRuntime ().exec (“/bin/bash”); with the lines ProcessBuilder builder = new ProcessBuilder(“/bin/bash”); builder.redirectErrorStream(true); Process process = builder.start(); ProcessBuilder is new in Java 5 and makes running external processes easier. In my opinion, its most significant improvement over Runtime.getRuntime().exec() is that it allows you to redirect the … Read more