IO和序列化
本文列举一些Web常用API,并展开扩展。
IO
1.对文件夹/文件 检查、新增、复制、移动、删除
using System;
using System.Configuration;
using System.IO;
using System.Text;
namespace IOSerialize.IO
{
/// <summary>
/// 文件夹 文件管理
/// </summary>
public class MyIO
{
/// <summary>
/// 配置绝对路径
/// </summary>
private static string LogPath = ConfigurationManager.AppSettings["LogPath"];
private static string LogMovePath = ConfigurationManager.AppSettings["LogMovePath"];
/// <summary>
/// 获取当前程序路径
/// </summary>
private static string LogPath2 = AppDomain.CurrentDomain.BaseDirectory;
/// <summary>
/// 读取文件夹 文件信息
/// </summary>
public static void Show()
{
{//check
if (!Directory.Exists(LogPath))//检测文件夹是否存在
{
}
DirectoryInfo directory = new DirectoryInfo(LogPath);//不存在不报错 注意exists属性
Console.WriteLine(string.Format("{0} {1} {2}", directory.FullName, directory.CreationTime, directory.LastWriteTime));
if (!File.Exists(Path.Combine(LogPath, "info.txt")))
{
}
FileInfo fileInfo = new FileInfo(Path.Combine(LogPath, "info.txt"));
Console.WriteLine(string.Format("{0} {1} {2}", fileInfo.FullName, fileInfo.CreationTime, fileInfo.LastWriteTime));
}
{//Directory
if (!Directory.Exists(LogPath))
{
DirectoryInfo directoryInfo = Directory.CreateDirectory(LogPath);//一次性创建全部的子路径
Directory.Move(LogPath, LogMovePath);//移动 原文件夹就不在了
Directory.Delete(LogMovePath);//删除
}
}
{//File
string fileName = Path.Combine(LogPath, "log.txt");
string fileNameCopy = Path.Combine(LogPath, "logCopy.txt");
string fileNameMove = Path.Combine(LogPath, "logMove.txt");
bool isExists = File.Exists(fileName);
if (!isExists)
{
Directory.CreateDirectory(LogPath);//创建了文件夹之后,才能创建里面的文件
using (FileStream fileStream = File.Create(fileName))//打开文件流 (创建文件并写入)
{
string name = "12345567778890";
byte[] bytes = Encoding.Default.GetBytes(name);
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Flush();
}
using (FileStream fileStream = File.Create(fileName))//打开文件流 (创建文件并写入)
{
StreamWriter sw = new StreamWriter(fileStream);
sw.WriteLine("1234567890");
sw.Flush();
}
using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
{
string msg = "今天是Course6IOSerialize,今天上课的人有55个人";
sw.WriteLine(msg);
sw.Flush();
}
using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
{
string name = "0987654321";
byte[] bytes = Encoding.Default.GetBytes(name);
sw.BaseStream.Write(bytes, 0, bytes.Length);
sw.Flush();
}
foreach (string result in File.ReadAllLines(fileName))
{
Console.WriteLine(result);
}
string sResult = File.ReadAllText(fileName);
Byte[] byteContent = File.ReadAllBytes(fileName);
string sResultByte = System.Text.Encoding.UTF8.GetString(byteContent);
using (FileStream stream = File.OpenRead(fileName))//分批读取
{
int length = 5;
int result = 0;
do
{
byte[] bytes = new byte[length];
result = stream.Read(bytes, 0, 5);
for (int i = 0; i < result; i++)
{
Console.WriteLine(bytes[i].ToString());
}
}
while (length == result);
}
File.Copy(fileName, fileNameCopy);
File.Move(fileName, fileNameMove);
File.Delete(fileNameCopy);
File.Delete(fileNameMove);//尽量不要delete
}
}
{//DriveInfo
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.IsReady)
Console.WriteLine("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}", drive.DriveType, drive.VolumeLabel, drive.Name, drive.TotalSize, drive.TotalFreeSpace);
else
Console.WriteLine("类型:{0} is not ready", drive.DriveType);
}
}
{
Console.WriteLine(Path.GetDirectoryName(LogPath)); //返回目录名,需要注意路径末尾是否有反斜杠对结果是有影响的
Console.WriteLine(Path.GetDirectoryName(@"d:\\abc")); //将返回 d:\
Console.WriteLine(Path.GetDirectoryName(@"d:\\abc\"));// 将返回 d:\abc
Console.WriteLine(Path.GetRandomFileName());//将返回随机的文件名
Console.WriteLine(Path.GetFileNameWithoutExtension("d:\\abc.txt"));// 将返回abc
Console.WriteLine(Path.GetInvalidPathChars());// 将返回禁止在路径中使用的字符
Console.WriteLine(Path.GetInvalidFileNameChars());//将返回禁止在文件名中使用的字符
Console.WriteLine(Path.Combine(LogPath, "log.txt"));//合并两个路径
}
}
/// <summary>
/// 1 try catch旨在上端使用,保证对用户的展示
/// 2 下端不要吞掉异常,隐藏错误是没有意义的,抓住再throw也没意义
/// 3 除非这个异常对流程没有影响或者你要单独处理这个异常
/// </summary>
/// <param name="msg"></param>
public static void Log(string msg)
{
StreamWriter sw = null;
try
{
string fileName = "log.txt";
string totalPath = Path.Combine(LogPath, fileName);
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
sw = File.AppendText(totalPath);
sw.WriteLine(string.Format("{0}:{1}", DateTime.Now, msg));
sw.WriteLine("***************************************************");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);//log
//throw ex;
//throw new exception("这里异常");
}
finally
{
if (sw != null)
{
sw.Flush();
sw.Close();
sw.Dispose();
}
}
}
}
}
- 绘图,验证码
using System;
using System.Configuration;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace IOSerialize.IO
{
/// <summary>
/// 绘图
/// </summary>
public class ImageHelper
{
private static string ImagePath = ConfigurationManager.AppSettings["ImagePath"];
private static string VerifyPath = ConfigurationManager.AppSettings["ImagePath"];
//绘图的原理很简单:Bitmap就像一张画布,Graphics如同画图的手,把Pen或Brush等绘图对象画在Bitmap这张画布上
/// <summary>
/// 画验证码
/// </summary>
public static void Drawing()
{
Bitmap bitmapobj = new Bitmap(100, 100);
//在Bitmap上创建一个新的Graphics对象
Graphics g = Graphics.FromImage(bitmapobj);
//创建绘画对象,如Pen,Brush等
Pen redPen = new Pen(Color.Red, 8);
g.Clear(Color.White);
//绘制图形
g.DrawLine(redPen, 50, 20, 500, 20);
g.DrawEllipse(Pens.Black, new Rectangle(0, 0, 200, 100));//画椭圆
g.DrawArc(Pens.Black, new Rectangle(0, 0, 100, 100), 60, 180);//画弧线
g.DrawLine(Pens.Black, 10, 10, 100, 100);//画直线
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 100, 200));//画矩形
g.DrawString("我爱北京天安门", new Font("微软雅黑", 12), new SolidBrush(Color.Red), new PointF(10, 10));//画字符串
//g.DrawImage(
if (!Directory.Exists(ImagePath))
Directory.CreateDirectory(ImagePath);
bitmapobj.Save(ImagePath + "pic1.jpg", ImageFormat.Jpeg);
//释放所有对象
bitmapobj.Dispose();
g.Dispose();
}
public static void VerificationCode()
{
Bitmap bitmapobj = new Bitmap(300, 300);
//在Bitmap上创建一个新的Graphics对象
Graphics g = Graphics.FromImage(bitmapobj);
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 150, 50));//画矩形
g.FillRectangle(Brushes.White, new Rectangle(1, 1, 149, 49));
g.DrawArc(Pens.Blue, new Rectangle(10, 10, 140, 10), 150, 90);//干扰线
string[] arrStr = new string[] { "我", "们", "孝", "行", "白", "到", "国", "中", "来", "真" };
Random r = new Random();
int i;
for (int j = 0; j < 4; j++)
{
i = r.Next(10);
g.DrawString(arrStr[i], new Font("微软雅黑", 15), Brushes.Red, new PointF(j * 30, 10));
}
bitmapobj.Save(Path.Combine(VerifyPath, "Verif.jpg"), ImageFormat.Jpeg);
bitmapobj.Dispose();
g.Dispose();
}
/// <summary>
/// 按比例缩放,图片不会变形,会优先满足原图和最大长宽比例最高的一项
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
public static void CompressPercent(string oldPath, string newPath, int maxWidth, int maxHeight)
{
Image _sourceImg = Image.FromFile(oldPath);
double _newW = (double)maxWidth;
double _newH = (double)maxHeight;
double percentWidth = (double)_sourceImg.Width > maxWidth ? (double)maxWidth : (double)_sourceImg.Width;
if ((double)_sourceImg.Height * (double)percentWidth / (double)_sourceImg.Width > (double)maxHeight)
{
_newH = (double)maxHeight;
_newW = (double)maxHeight / (double)_sourceImg.Height * (double)_sourceImg.Width;
}
else
{
_newW = percentWidth;
_newH = (percentWidth / (double)_sourceImg.Width) * (double)_sourceImg.Height;
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH);
Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel);
_sourceImg.Dispose();
g.Dispose();
bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
/// <summary>
/// 按照指定大小对图片进行缩放,可能会图片变形
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
public static void ImageChangeBySize(string oldPath, string newPath, int newWidth, int newHeight)
{
Image sourceImg = Image.FromFile(oldPath);
System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight);
Graphics g = Graphics.FromImage(bitmap);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.Clear(Color.Transparent);
g.DrawImage(sourceImg, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), GraphicsUnit.Pixel);
sourceImg.Dispose();
g.Dispose();
bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
bitmap.Dispose();
}
}
}
序列化
序列化是把一个内存中的对象的信息转化成一个可以持久化保存的形式,以便于保存或传输,序列化的主要作用是不同平台之间进行通信,常用的有序列化有xml、json、SOAP、二进制文件等,下面就逐个讲下这三种序列化的方法。
XML
XML即可扩展标记语言(eXtensible Markup Language),被设计用来结构化、存储以及传输信息。标记是指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。如何定义这些标记,既可以选择国际通用的标记语言,比如HTML,也可以使用像XML这样由相关人士自由决定的标记语言,这就是语言的可扩展性。XML是从SGML中简化修改出来的。它主要用到的有XML、XSL和XPath等。
C#中将对象序列化和反序列化为xml的类是XmlSerializer,要引用System.Xml.Serialization。先创建一个XmlSerializer对象实例,然后用实例的Serialize的方法将对象写入到文件流中,代码如下:
using System.Xml.Serialization;
/// <summary>
/// XML序列化器
/// </summary>
public static void XmlSerialize()
{
//使用XML序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"Student.xml");//文件名称与路径
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
List<Programmer> pList = DataFactory.BuildProgrammerList();
XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//创建XML序列化器,需要指定对象的类型
xmlFormat.Serialize(fStream, pList);
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>));//创建XML序列化器,需要指定对象的类型
//使用XML反序列化对象
fStream.Position = 0;//重置流位置
List<Programmer> pList = pList = (List<Programmer>)xmlFormat.Deserialize(fStream);
}
}
另外,注意,我们需要序列化的对象必须实现Serializable特性,才可以被序列化,如下:
[Serializable] //必须添加序列化特性
public class Person
{
[NonSerialized]
public int Id = 1;
public string Name { get; set; }
public string Sex { get; set; }
}
[Serializable] //必须添加序列化特性
public class Programmer : Person
{
private string Language { get; set; }//编程语言
public string Description { get; set; }
}
json
JSON:JavaScript对象表示法(JavaScript Object Notation), 是一种轻量级的数据交换格式, 易于人阅读和编写, 同时也易于机器解析和生成。JSON是存储和交换文本信息的语法,类似XML。
C#中用于对象和json相互转换的原生类有两个:Newtonsoft和JavaScriptSerializer,其中JavaScriptSerializer主要用于web的浏览器和服务器之间的通信。
using System.Collections.Generic;
using System.Web.Script.Serialization;//System.Web.Extensions
using Newtonsoft.Json;//nuget Newtonsoft
public class JsonHelper
{
#region Json
/// <summary>
/// JavaScriptSerializer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ObjectToString<T>(T obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(obj);
}
/// <summary>
/// JavaScriptSerializer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <returns></returns>
public static T StringToObject<T>(string content)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<T>(content);
}
/// <summary>
/// JsonConvert.SerializeObject
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string ToJson<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
/// <summary>
/// JsonConvert.DeserializeObject
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="content"></param>
/// <returns></returns>
public static T ToObject<T>(string content)
{
return JsonConvert.DeserializeObject<T>(content);
}
#endregion Json
}
public class program {
/// <summary>
/// json也可以的
/// </summary>
public static void Json()
{
List<Programmer> pList = DataFactory.BuildProgrammerList();
string result = JsonHelper.ObjectToString<List<Programmer>>(pList);
List<Programmer> pList1 = JsonHelper.StringToObject<List<Programmer>>(result);
}
}
SOAP
SOAP即简单对象访问协议(Simple Object Access Protocol,SOAP),是一种轻量的、简单的、基于XML的协议,它被设计成在WEB上交换结构化的和固化的信息。 SOAP可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。SOAP使用基于XML的数据结构和超文本传输协议(HTTP)的组合定义了一个标准的方法来使用Internet上各种不同操作环境中的分布式对象。
using System.Runtime.Serialization.Formatters.Soap;
/// <summary>
/// soap序列化器
/// </summary>
public static void SoapSerialize()
{
//使用Soap序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"SoapSerialize.txt");//文件名称与路径
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
List<Programmer> pList = DataFactory.BuildProgrammerList();
SoapFormatter soapFormat = new SoapFormatter();//创建二进制序列化器
//soapFormat.Serialize(fStream, list);//SOAP不能序列化泛型对象
soapFormat.Serialize(fStream, pList.ToArray());
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
SoapFormatter soapFormat = new SoapFormatter();//创建二进制序列化器
//使用二进制反序列化对象
fStream.Position = 0;//重置流位置
List<Programmer> pList = ((Programmer[])soapFormat.Deserialize(fStream)).ToList();//反序列化对象
}
}
注意,SOAP序列化不能序列化泛型类型。
二进制文件
C#中将对象序列化和反序列化为二进制文件的类是BinaryFormatter,要引用System.Runtime.Serialization.Formatters.Binary
先创建一个BinaryFormatter对象实例,然后用实例的Serialize的方法将对象写入到文件流中,代码如下:
/// <summary>
/// 二进制序列化器
/// </summary>
public static void BinarySerialize()
{
//使用二进制序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"BinarySerialize.txt");//文件名称与路径
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{//需要一个stream,这里是直接写入文件了
List<Programmer> pList = DataFactory.BuildProgrammerList();
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
binFormat.Serialize(fStream, pList);
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{//需要一个stream,这里是来源于文件
BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器
//使用二进制反序列化对象
fStream.Position = 0;//重置流位置
List<Programmer> pList = (List<Programmer>)binFormat.Deserialize(fStream);//反序列化对象
}
}
JSON与XML的区别
- JSON 比 XML 更小、更快,更易解析。
- XML文档可以附上DTD、Schema,还有一堆的诸如XPath之类规范,使用自定义XML元素或属性,能很方便地给数据附加各种约束条件和关联额外信息,从数据表达能力上看,XML强于Json,但是很多场景并不需要这么复杂的重量级的东西。
参考:
C# 序列化与反序列化