using Nancy.Json;
Person p1 = new Person();
p1.Name = “zs”;
// json 序列化
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string json = javaScriptSerializer.Serialize(p1);
Console.WriteLine(json);
Console.ReadKey();
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
1, 序列化只把对象的存储格式改变了,对象的实际存储内容不变
2,序列化只序列化数据 (例如:字段的值,属性的值)
1, 被序列化的对象必须标记为可序列化
加Serializable
2, 被序列化的所有父类都必须标记为可序列化
二进制序列化是把对象变成流的过程,就是把对象变成byte[]
序列化后,保存到磁盘上,操作磁盘的文件需要文件流 FileStream
3, 要求被 序列化的对象的类型中的所有字段 的类型也必须标记为"可序列化"
//1, 创建序列化器
BinaryFormatter binaryFormatter = new BinaryFormatter();
// 创建文件流
using(FileStream fs = new FileStream(“Person.bf”, FileMode.Create))
{
//开始序列化
binaryFormatter.Serialize(fs, p1);
}
Console.WriteLine(“序列化OK”);
Console.ReadKey();
[Serializable]
public class Person:Animal
{
public int Id { get; set; }
[NonSerialized]
private string _name;
public string Name
{ get { return _name; } set { _name = value; }
}
public int Age { get; set; }
public Car BM { get; set; }
}
[Serializable]
public class Animal
{
public void Move()
{
Console.WriteLine(“会动”);
}
}
[Serializable]
public class Car
{
}
using System.Runtime.Serialization.Formatters.Binary;
BinaryFormatter binaryFormatter = new BinaryFormatter();
using(FileStream fs = new FileStream(“Person.bf”, FileMode.Open))
{
object obj = binaryFormatter.Deserialize(fs);
Person person = obj as Person;
Console.WriteLine(person.Age);
Console.WriteLine(person.Name);
}
Console.WriteLine(“反序列化成功!”);
Console.ReadKey();
序列化时不建议使用自动属性
代码:
string path = @“D:\MVC\MVCDemo”;
LoadDate(path, treeView1.Nodes);
private void LoadDate(string path, TreeNodeCollection nodes)
{
// 获取path 下的所有文件夹
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
TreeNode tnode = nodes.Add(dir);
LoadDate(dir, tnode.Nodes);
}
// 获取path 下的所有文本文件
string[] files = Directory.GetFiles(path, “*.txt”);
foreach (string file in files)
{
TreeNode node = nodes.Add(Path.GetFileName(file));
node.Tag = file;
}
}
using System.Text;
EncodingInfo[] infos = Encoding.GetEncodings();
foreach (var item in infos)
{
Console.WriteLine($“{item.CodePage}—{item.DisplayName}----{item.Name}”);
}
Console.ReadKey();
例子:大文件拷贝
string source = @“D:\迅雷下载\囧妈.mp4”;
string target = @“D:\迅雷下载\new\囧妈.mp4”;
BigFileCopy(source, target);
void BigFileCopy(string source, string target)
{
// 创建 一个读取源文件
using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read))
{
// 创建一个写入新文件
using (FileStream sr = new FileStream(target, FileMode.Create, FileAccess.Write))
{
// 缓存区
byte[] bytes = new byte[1024 * 1024 * 5];
// 返回值表示本次实际读的字节个数
int r = fs.Read(bytes, 0, bytes.Length);
while (r > 0)
{
sr.Write(bytes, 0, r);
Console.WriteLine(Math.Round((decimal)sr.Position / fs.Length, 2) * 100);
r = fs.Read(bytes, 0, bytes.Length);
}
// 将读取的内容写到新文件 中
}
}
}
using (StreamReader reader = new StreamReader(@“1.txt”, Encoding.Default))
{
//StreamReader是按行读文本文件
string line = null;
//while (!reader.EndOfStream)
//{
// line = reader.ReadLine();
//}
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
}
}