.NET基础加强第七课--文件流

序列化

JSON序列化

例子

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);

        }
        // 将读取的内容写到新文件 中


    }
}

}

StreamReader 操作

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);
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yuansheng888888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值