引言
      文件的传输和读写通常都离不开Byte,Stream,File这个类,这里我简单封装一下,方便使用.

帮助类

   

    public static class FileHelper
{ /// <summary>
///Stream转化成byte数组
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>
public static byte[] StreamToBytes(Stream stream)
{
MemoryStream memoryStream =new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray(); // MemoryStream memoryStream = new MemoryStream();
//byte[] btContent = new byte[512];
//int intSize = 0;
//intSize = stream.Read(btContent, 0, 512);
//while (intSize > 0)
//{
// memoryStream.Write(btContent, 0, intSize);
// intSize = stream.Read(btContent, 0, 512);
//}
// return memoryStream.ToArray();
} /// <summary>
///byte数组转化成Stream
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static Stream BytesToStream(byte[] bytes)
{
return new MemoryStream(bytes);
} /// <summary>
///将流保存成文件
/// </summary>
/// <param name="stream">流</param>
/// <param name="filepath">保存文件的路径</param>
public static void StreamToFile(Stream stream,string filepath)
{
byte[] bytes = StreamToBytes(stream);
FileStream fileStream = new FileStream(filepath, FileMode.Create);
fileStream.Write(bytes, , bytes.Length);
fileStream.Flush();
fileStream.Close();
} /// <summary>
///将数组保存成文件
/// </summary>
/// <param name="bytes">byte数组</param>
/// <param name="filepath">保存文件的路径</param>
public static void BytesToFile(byte[] bytes, string filepath)
{
//FileStream fileStream = new FileStream(filepath, FileMode.Create);
//fileStream.Write(bytes, 0, bytes.Length);
//fileStream.Flush();
//fileStream.Close(); File.WriteAllBytes(filepath, bytes); } /// <summary>
///读取文件转化成流
/// </summary>
/// <param name="filepath">文件路径</param>
/// <returns></returns>
public static Stream FileToStream(string filepath)
{
FileStream fileStream = new FileStream(filepath, FileMode.Open);
MemoryStream memoryStream = new MemoryStream();
fileStream.CopyTo(memoryStream);
fileStream.Close();
memoryStream.Position = ;
return memoryStream;
} /// <summary>
///读取文件转成问Byte数组
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
public static byte[] FileToBytes(string filepath)
{
//FileStream fileStream = new FileStream(filepath, FileMode.Open);
//byte[] bytes= StreamToBytes(fileStream);
//fileStream.Close();
//return bytes; return File.ReadAllBytes(filepath);
} /// <summary>
/// 读取文件转化成字符串
/// </summary>
/// <param name="filepath">文件路径</param>
/// <param name="encode"></param>
/// <returns>字符串</returns>
public static string FileToString(string filepath, Encoding encode)
{
//StreamReader streamReader = new StreamReader(filepath, encode);
//string text = streamReader.ReadToEnd();
//streamReader.Close(); string text = File.ReadAllText(filepath, encode);
return text;
} /// <summary>
/// 流转化成字符串
/// </summary>
/// <param name="stream">流</param>
/// <param name="encode"></param>
/// <returns>字符串</returns>
public static string StreamToString(Stream stream, Encoding encode)
{
if (stream.CanSeek)
{
stream.Position = ;
}
StreamReader streamReader = new StreamReader(stream,encode);
string text =streamReader.ReadToEnd();
streamReader.Close();
return text;
} /// <summary>
/// byte数组转化字符串
/// </summary>
/// <param name="bytes">byte数组</param>
/// <param name="encode">编码</param>
/// <returns></returns>
public static string BytesToString(byte[] bytes, Encoding encode)
{
return encode.GetString(bytes);
} /// <summary>
/// 字符串转化byte数组
/// </summary>
/// <param name="text">字符串</param>
/// <param name="encode">编码</param>
/// <returns></returns>
public static byte[] StringToBytes(string text, Encoding encode)
{
return encode.GetBytes(text);
} /// <summary>
/// 字符串转化成流
/// </summary>
/// <param name="text">字符串</param>
/// <param name="encode">编码</param>
/// <returns></returns>
public static Stream StringToStream(string text, Encoding encode)
{
var bytes= StringToBytes(text, encode);
return BytesToStream(bytes);
} /// <summary>
/// 往文件中追加文本内容
/// </summary>
/// <param name="text">文本内容</param>
/// <param name="filepath">文件路径</param>
/// <param name="encode">编码</param>
public static void AppendStringToFile(string text, string filepath, Encoding encode)
{
File.AppendAllText(filepath, Environment.NewLine + text, encode);
} }

【.Net】Byte,Stream,File的转换的更多相关文章

  1. C# 流与文件(Stream & File & byte[])

    原文:https://www.cnblogs.com/long-gengyun/archive/2010/03/28/1698681.html 文件概述  文件在操作时表现为流,即流是从一些输入中读取 ...

  2. 转转转--Java File和byte数据之间的转换

    package cn.iworker.file; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; ...

  3. Java 学习(9):java Stream & File & IO

    Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...

  4. BufferHelp byte[] Stream string FileStream Image Bitmap

    /******* * *** ***** ** ** * * * * * * * * ***** * * * * * * * * * * * * * * * ******* *** * ***** * ...

  5. stream,file,filestream,memorystream简单的整理

    一.Stream 什么是stream?(https://www.cnblogs.com/JimmyZheng/archive/2012/03/17/2402814.html#no8) 定义:提供字节序 ...

  6. Serializable 序列化 The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform.

    Java 序列化接口Serializable详解 - 火星猿类 - 博客园 http://www.cnblogs.com/tomtiantao/p/6866083.html 深入理解JAVA序列化 - ...

  7. C# Byte[] 转String 无损转换

    C# Byte[] 转String 无损转换 转载请注明出处 http://www.cnblogs.com/Huerye/ /// <summary> /// string 转成byte[ ...

  8. OpenCV中IplImage图像格式与BYTE图像数据的转换

    最近在将Karlsruhe Institute of Technology的Andreas Geiger发表在ACCV2010上的Efficent Large-Scale Stereo Matchin ...

  9. 关于C# byte[]与struct的转换

    转自:http://blog.chinaunix.net/uid-215617-id-2213082.html Some of the C# code I've been writing recent ...

随机推荐

  1. mybatis-generator和TKmybatis的结合使用

    mybatis-generator可以自动生成mapper和entity文件,mybatis-generator有三种用法:命令行.eclipse插件.maven插件.这里使用的是maven插件方式, ...

  2. 字符串之strcmp

    功能:比较两个字符串的ascII码大小 输入:两个字符串 返回值:相等为0,大于为大于零,小于为小于零 #include <iostream> #include <assert.h& ...

  3. 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related(转)

    转至:http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html application/x-www-form-urlencoded: 窗 ...

  4. Linux 使用pppd和ppp程序进行3G/4G拨号

    试验环境:Linux marsboard 3.4.90 #9 SMP PREEMPT Thu Mar 3 18:28:43 CST 2016 armv7l armv7l armv7l GNU/Linu ...

  5. XP、win7下Excel 2007多窗口打开Excel的解决方法

    http://blog.x6x8.com/?p=330 Win7下的方法(借鉴了XP的方法): 1.先在桌面先建一个txt文件,将下面的代码复制进去 32位WIN7复制下面一行start “Excel ...

  6. android 对话框中的进度条 (ProgressDialog)

    from:http://byandby.iteye.com/blog/817214 显然要定义对话框进度条就要用ProgressDialog,首先我们需要创建ProgressDialog对象,当然这里 ...

  7. paramiko 模块安装和使用

    一.Centos安装Paramiko 1.安装组件 yum install openssl openssl-devel python-dev pycrypto -y yum install zlib- ...

  8. C++ IPv4与IPv6的兼容编码(转,出自http://blog.csdn.net/ligt0610/article/details/18667595)

    这里不再对IPv6 socket相关编程的基础知识进行讲解,只提供一个IP协议无关的服务端和客户端的代码,仅供参考. 服务端代码: #include <iostream> #include ...

  9. 【鸟哥的Linux私房菜】笔记2

    Linux的应用 学习资源整理 安装记录 >< 1.Linux的应用: 网络服务器 数据库 学术机构的高效运算任务 嵌入式系统 ... 2.挂载与磁盘分区 学习资源整理 学习 1.书上的网 ...

  10. Keepalived + LVS/DR 安装配置

    Keepalived + LVS/DR 说明 Keepalived:可以踢出掉故障服务 Keepalived:可以实现主从切换,解决单点故障 实验环境 四台主机:Linux Centos 6.4 32 ...