/// <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>
     /// 关于异常处理:(链接:http://pan.baidu.com/s/1kVNorpd 密码:pyhv)
     /// 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();
             }
         }
     }

}

学习笔记: IO操作及序列化的更多相关文章

  1. Java学习笔记——IO操作之对象序列化及反序列化

    对象序列化的概念 对象序列化使得一个程序可以把一个完整的对象写到一个字节流里面:其逆过程则是从一个字节流里面读出一个事先存储在里面的完整的对象,称为对象的反序列化. 将一个对象保存到永久存储设备上称为 ...

  2. Java学习笔记——IO操作之以图片地址下载图片

    以图片地址下载图片 读取给定图片文件的内容,用FileInputStream public static byte[] mReaderPicture(String filePath) { byte[] ...

  3. Javascript学习笔记二——操作DOM

    Javascript学习笔记 DOM操作: 一.GetElementById() ID在HTML是唯一的,getElementById()可以定位唯一的一个DOM节点 二.querySelector( ...

  4. MongoDB学习笔记:Python 操作MongoDB

    MongoDB学习笔记:Python 操作MongoDB   Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...

  5. Javascript学习笔记三——操作DOM(二)

    Javascript学习笔记 在我的上一个博客讲了对于DOM的基本操作内容,这篇继续巩固一下对于DOM的更新,插入和删除的操作. 对于HTML解析的DOM树来说,我们肯定会时不时对其进行一些更改,在原 ...

  6. AI学习---数据IO操作&神经网络基础

    数据IO操作 TF支持3种文件读取:    1.直接把数据保存到变量中    2.占位符配合feed_dict使用    3. QueueRunner(TF中特有的) 文件读取流程 文件读取流程(多线 ...

  7. python学习笔记 IO 文件读写

    读写文件是最常见的IO操作.python内置了读写文件的函数. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统完成的,现代操作系统不允许普通的程序直接对磁盘进行操作,所以, 读写 ...

  8. Java 学习笔记 IO流与File操作

    可能你只想简单的使用,暂时不想了解太多的知识,那么请看这里,了解一下如何读文件,写文件 读文件示例代码 File file = new File("D:\\test\\t.txt" ...

  9. java学习笔记--IO流

    第十二章大纲: I/O input/output 输入/输出 一.创建文件,借助File类来实现 file.createNewFile() : 创建文件 file.exists() : 判断文件是否存 ...

随机推荐

  1. MD5 与 SHA 在 Delphi 中函数实现,加密密码

    MD5 与 SHA 在 Delphi 中函数实现. 为了加密密码,必须使用一种算法,查询资料,比较好的方法是使用:MD5等算法,参考:Delphi XE8 支持MD5 第一种方式是:引用 System ...

  2. python标准库之argparse

    argparse的使用 argparse 是 Python 内置的一个用于命令项选项与参数解析的模块,通过在程序中定义好我们需要的参数,argparse 将会从 sys.argv 中解析出这些参数,并 ...

  3. Linux内核初探

    内存管理之内存寻址 内存管理是迄今为止Unix内核中最复杂的活动 虚拟内存: 所有新近的Unix系统都提供一种有用的抽象, 叫作虚拟内存(virtual memory): 虚拟内存可以理解为一种逻辑层 ...

  4. js 常用的工具函数

    1 类型判断 isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } i ...

  5. LabVIEW 获取本机多个ip地址

    图 1   网上见了好多设置的,都没讲清楚,在这里整理一下本机ip地址的获取问题.关键在"字符串向ip地址转换"函数的设置上面,见下图2,选择多输出就能获取本机的多个ip地址,若不 ...

  6. java后台发送请求并获取返回值(续)

    在java后端发送请求给另一个平台,从而给前端实现 "透传"的过程中,出现:数据请求到了并传到了前端,但是控制台打印时中文显示Unicode码而前端界面中中文显示不出来!!!开始怀 ...

  7. Ex 2_16 给定一个无穷数组..._第二次作业

    先比较数组的A[0]元素,若不相等接下来比较A[1],A[2],A[4],A[8]…,若找到一个区间A[2n-1]<x<A[2n],再对这个区间进行折半查找操作.总的时间为O(logn). ...

  8. webstorm2017.3最新激活教程(激活码、注册码)亲测成功

    1 前言 webstorm2017.3最新激活教程(旧版本可用,亲测成功,有疑问随时联系),20180411测试成功,还是注册机靠谱~ 2 下载地址 链接: https://pan.baidu.com ...

  9. tp5结合FormData实现ajax文件上传

    或者使用: 下面使用jquery.form.js的表单插件来提交表单

  10. Centos7 设置静态IP地址

    一:  修改网卡配置文件(操作前先备份一下该文件),/etc/sysconfig/network-scripts/ 具体操作如下: 1:进入修改目录 [root@localhost ~]# clear ...