CSharp - c#如何将流保存到文件?

  显示原文与译文双语对照的内容

我使用流初始化了一个 StreamReader 对象,现在我想将这个流保存到磁盘( 流可能是 .gif 或者 .jpg 或者 .pdf ) 。

现有代码:


StreamReader sr = new StreamReader(myOtherObject.InputStream);

  1. 我需要将它保存到磁盘( 我有文件名) 。
  2. 将来我可能想把它存储到 SQL Server 。

我也有的编码类型,这样我需要如果我将文件存储到 SQL Server,正确?

时间:

作为由Tilendor乔恩回答抛靶的双向飞碟,流有一个 CopyTo 中突出显示的方法因为. NET 4.


var fileStream = File.Create("C:PathToFile");
myOtherObject.InputStream.Seek(0, SeekOrigin.Begin);
myOtherObject.InputStream.CopyTo(fileStream);
fileStream.Close();

或者使用 using 语法:


using (var fileStream = File.Create("C:PathToFile"))
{
 myOtherObject.InputStream.Seek(0, SeekOrigin.Begin);
 myOtherObject.InputStream.CopyTo(fileStream);
}

你于二进制文件相关不准使用 StreamReader 是用于文本的数据。 于任意二进制data,相关你将几乎当然 丢失数据如果使用。罗伊 ( 如果你使用 Encoding.GetEncoding(28591),你可能会没事,但重点是什么)?

为什么你需要使用 StreamReader? 为什么不干脆将二进制数据作为二进制数据,并把它写回磁盘( 或者 SQL ) 作为二进制数据?

编辑:这似乎是人们想要看到的东西。 如果你做只是想将一个流复制到另一个( 如, 对一个文件使用类似这样的内容:


///<summary>
///Copies the contents of input to output. Doesn't close either stream.
///</summary>
public static void CopyStream(Stream input, Stream output)
{
 byte[] buffer = new byte[8 * 1024];
 int len;
 while ( (len = input.Read(buffer, 0, buffer.Length))> 0)
 {
 output.Write(buffer, 0, len);
 } 
}

使用它将流转储到文件,例如:


using (Stream file = File.Create(filename))
{
 CopyStream(input, file);
}


public void CopyStream(Stream stream, string destPath)
{
 using (var fileStream = new FileStream(destPath, FileMode.Create, FileAccess.Write))
 {
 stream.CopyTo(fileStream);
 }
}


//If you don't have. Net 4.0 :)

public void SaveStreamToFile(Stream stream, string filename)
{ 
 using(Stream destination = File.Create(filename))
 Write(stream, destination);
}

//Typically I implement this Write method as a Stream extension method. 
//The framework handles buffering.

public void Write(Stream from, Stream to)
{
 for(int a = from.ReadByte(); a!= -1; a = from.ReadByte())
 to.WriteByte( (byte) a );
}

/*
Note, StreamReader is an IEnumerable<Char> while Stream is an IEnumbable<byte>.
The distinction is significant such as in multiple byte character encodings 
like Unicode used in. Net where Char is one or more bytes (byte[n]). Also, the
resulting translation from IEnumerable<byte> to IEnumerable<Char> can loose bytes
or insert them (for example,"n" vs."rn") depending on the StreamReader instance
CurrentEncoding.
*/

为什么不使用FileStream对象?


public void SaveStreamToFile(string fileFullPath, Stream stream)
{
 if (stream.Length == 0) return;

//Create a FileStream object to write a stream to a file
 using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length))
 {
//Fill the bytes[] array with the stream data
 byte[] bytesInStream = new byte[stream.Length];
 stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

//Use FileStream object to write to the specified file
 fileStream.Write(bytesInStream, 0, bytesInStream.Length);
 }
}


public void testdownload(stream input)
{
 byte[] buffer = new byte[16345];
 using (FileStream fs = new FileStream(this.FullLocalFilePath,
 FileMode.Create, FileAccess.Write, FileShare.None))
 {
 int read;
 while ((read = input.Read(buffer, 0, buffer.Length))> 0)
 {
 fs.Write(buffer, 0, read);
 }
 }
}

另一个选项是将流获取到 byte[] 并使用 File.WriteAllBytes 。 这应该是:


using (var stream = new MemoryStream())
{
 input.CopyTo(stream);
 File.WriteAllBytes(file, stream.ToArray());
}

将它包装在扩展方法中,可以更好地命名:


public void WriteTo(this Stream input, string file)
{
//your fav write method:

 using (var stream = File.Create(file))
 {
 input.CopyTo(stream);
 }

//or

 using (var stream = new MemoryStream())
 {
 input.CopyTo(stream);
 File.WriteAllBytes(file, stream.ToArray());
 }

//whatever that fits.
}


private void SaveFileStream(String path, Stream stream)
{
 var fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
 stream.CopyTo(fileStream);
 fileStream.Dispose();
}

将 FTP memorystream转换为 filestream


foreach (var file in DirectoryList)
 {
 using (var memoryStream = new MemoryStream())
 {
 client.DownloadFile(file.Name, memoryStream);
 using (var fileStream = File.Create(@"C:temp" + file.Name))
 {
 memoryStream.WriteTo(fileStream);
 }
 }
 }

...