其他分享
首页 > 其他分享> > 大 文件操作 及 4种序列化 介绍

大 文件操作 及 4种序列化 介绍

作者:互联网

文件操作及4种序列化 介绍

1 文件夹/文件 检查、新增、复制、移动、删除,递归编程技巧
2 文件读写,记录文本日志,读取配置文件
3 三种序列化器,xml和json
1.二进制序列化:不支持跨平台,序列化生成文件体积小;
2.soapserialize序列化:跨平台,序列化生成文件体积大;
3.xml序列化:支持快平台,体积适中,容易阅读
4.json序列化:支持快平台,体积小,容易阅读;JsonConvert.SerializeObject(obj);
序列化,反序列化:不同应用间传递数据;
序列化:对象list到文本字符串的过程
反序列化:字符串到对象list的过程;
system.runtime.serilization.Formatters.Binary

4 验证码、图片缩放
二维码生成;
图片压缩
添加水印,二维码 qrcode
画布+壁画+字母数字

 

DriveInfo

Directory
DirectoryInfo
File
FileInfo
FileStream
StreamWriter

File.AppendText()
Flushz90
ReadAllLine()
ReadAllText()
ReadAllBytes()
大文件读取:stream.Read(bytes,0,5)
大文件写入: AppendText()
返回随机的文件名:Path.GetRandomFileName();
文本日志:异常处理

无法预估深度的,由重复的动作,用递归,自己调用自己
递归找出所有文件夹下面的文件夹
/// <summary>
/// 完成 文件夹--子目录--放入集合
/// </summary>
/// <param name="directoryList"></param>
/// <param name="directoryCurrent"></param>
/// <returns></returns>
private static List<DirectoryInfo> GetChild(List<DirectoryInfo> directoryList, DirectoryInfo directoryCurrent)
{
var childArray = directoryCurrent.GetDirectories();
if (childArray != null && childArray.Length > 0)
{
directoryList.AddRange(childArray);
foreach (var child in childArray)
{
GetChild(directoryList, child);
}
}
return directoryList;
}

 

-------------------

{//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"));//合并两个路径
}
}

----------log-------------

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

标签:文件,Console,WriteLine,LogPath,sw,介绍,File,Path,序列化
来源: https://www.cnblogs.com/csj007523/p/14328040.html