• 20151024_003_C#基础知识(File / FileStream / StreamReader/StreamWriter)


    1:绝对路径和相对路径

    绝对路径:通过给定的路径直接能在我的电脑中找到这个文件。

    相对路径:文件相对于应用程序的路径。

    2:编码格式

    乱码:产生乱码的原因,就是你保存这个文件所采用的编码,跟你打开这个文件所采用的编码格式不一样。(GB2312:简体中文; GBK:简体中文+繁体中文)

    UTF:Universal TransFormation Format

    目前计算机中用得最广泛的字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Standard Code for Information Interchange,美国标准信息交换码),

    它已被国际标准化组织(ISO)定为国际标准,称为ISO 646标准。适用于所有拉丁文字字母,ASCII码有7位码和8位码两种形式。

    Unicode码扩展自ASCII字元集。在严格的ASCII中,每个字元用7位元表示,或者电脑上普遍使用的每字元有8位元宽;而Unicode使用全16位元字元集。

    这使得Unicode能够表示世界上所有的书写语言中可能用於电脑通讯的字元、象形文字和其他符号。

    Unicode最初打算作为ASCII的补充,可能的话,最终将代替它。考虑到ASCII是电脑中最具支配地位的标准,所以这的确是一个很高的目标。

    UTF-8是UNICODE的一种变长字符编码又称万国码,由Ken Thompson于1992年创建。现在已经标准化为RFC 3629。UTF-8用1到6个字节编码UNICODE字符。

    用在网页上可以同一页面显示中文简体繁体及其它语言(如日文,韩文)

    3:File类

    using System.IO; 静态类

    File类只能用来读取小文件(读取的内容放在内存中)。

    File.Create(path); //创建文件对象

    File.Delete(path); //删除一个文件

    File.Copy(oldPath,newPath); //复制一个文件

    File.Exist(path); //path文件是否存在

    4:FileStream类: 操作字节的(可以是任何文件)

    using System.IO; 继承于Stream

    4.1:FileStream创建

          创建FileStream对象,将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源。

          流处理记得using或Close

    4.1.1:FileStream Read 流处理使用Close

     1 FileStream fsRead = new FileStream(@"C:UsersadminDesktopaaa.txt", FileMode.OpenOrCreate, FileAccess.Read);
     2 byte[] buffer = new byte[1024 * 1024 * 5];
     3 //返回本次实际读取到的有效字节数
     4 int r = fsRead.Read(buffer, 0, buffer.Length);
     5 //将字节数组中每一个元素按照指定的编码格式解码成字符串
     6 string s = Encoding.Default.GetString(buffer, 0, r);
     7 
     8 //关闭流
     9 fsRead.Close();
    10 //释放流所占用的资源
    11 fsRead.Dispose();
    12 Console.WriteLine(s);
    13 Console.ReadKey();
    FileStream Read(小文件读取)

    4.1.2:FileStream Read 流处理使用using

     1 using (FileStream fssRead = new FileStream(@"C:UsersadminDesktopaaa.txt", FileMode.OpenOrCreate, FileAccess.Read))
     2 {
     3     byte[] bufferr = new byte[1024 * 1024 * 5];
     4     //返回本次实际读取到的有效字节数
     5     int rr = fssRead.Read(bufferr, 0, bufferr.Length);
     6     //将字节数组中每一个元素按照指定的编码格式解码成字符串
     7     string ss = Encoding.Default.GetString(bufferr, 0, rr);
     8 
     9     Console.WriteLine(ss);
    10     Console.ReadKey();
    11 }
    FileStream Read(小文件读取)

    4.1.3:FileStream Write 流处理使用using

    1 using (FileStream fsWrite = new FileStream(@"C:UsersadminDesktopaaa.txt", FileMode.OpenOrCreate, FileAccess.Write))
    2 {
    3     string str = "准备覆盖了。。。。";
    4     byte[] buffer = Encoding.Default.GetBytes(str);
    5     fsWrite.Write(buffer, 0, buffer.Length);
    6 }
    7 Console.WriteLine("OK");
    8 Console.ReadKey();
    FileStream Write(小文件读取)

    4.1.4:FileStream,使用文件流实现多媒体文件(大文件)的读取

     1     class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             string sourcePath = @"C:UsersadminDesktopSourceFile.avi";
     6             string targetPath = @"C:UsersadminDesktopCopyFile.avi";
     7             CopyFile(sourcePath, targetPath);
     8             Console.WriteLine("CopyFile Success");
     9             Console.ReadKey();
    10         }
    11 
    12         static void CopyFile(string sourcePath, string targetPath)
    13         {
    14             using (FileStream fsRead = new FileStream(sourcePath, FileMode.OpenOrCreate, FileAccess.Read))
    15             {
    16                 using (FileStream fsWrite = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write))
    17                 {
    18                     //可能文件比较大,需要循环读取
    19                     while (true)
    20                     {
    21                         Byte[] buffer = new byte[1024 * 1024 * 5];
    22                         int r = fsRead.Read(buffer, 0, buffer.Length);
    23 
    24                         if (r == 0)
    25                         {
    26                             break;
    27                         }
    28                         fsWrite.Write(buffer, 0, r);
    29                     }
    30                 }
    31             }
    32         }
    33     }
    FileStream Read/Write(大文件读取)

    5:StreamReader / StreamWriter类:操作字符的(只能是文本文件)

    using System.IO; 继承于TextReader/TextWriter

    5.1:StreamReader Read txtFile

    1 using (StreamReader srRead = new StreamReader(@"C:UsersadminDesktopaaa.txt", Encoding.Default))
    2 {
    3     while (!srRead.EndOfStream)
    4     {
    5         Console.Write(srRead.ReadLine());
    6     }
    7 }
    StreamReader Read txtFile

    5.2:StreamWriter Write txtFile

    1 using (StreamWriter swWrite = new StreamWriter(@"C:UsersadminDesktopaaa.txt", false, Encoding.Default))
    2 {
    3     swWrite.WriteLine("今天天气好晴朗。。。。。");
    4 }
    5 Console.WriteLine("OK");
    6 Console.ReadKey();
    StreamWriter Write txtFile

    其它流NetworkStream / MemoryStream 

    6:Path类

    using System.IO; 静态类

    Path.GetFileName(); //获得文件的名字(包含扩展名)

    Path.GetFileNameWithoutExtension();获得文件的名字(没有扩展名)

    Path.GetExtension(); //获得文件的扩展名

    Path.GetDirectoryName();//获得文件所在的文件夹的名称

    Path.GetFullPath();//获得文件所在的全路径

    Path.Combine();//合并字符串作为路径

    7:Directory类

    using System.IO; 静态类

    Directory.GetFiles(path); //返回指定目录中的文件的名称

    Directory.CreateDirectory(path); //目录创建方法

    Directory.Delete(path); //目录删除方法

    Directory.Move(string sourceDirName, string destDirName); //目录移动方法

    Directory.GetDirectories(path); //获取当前目录下的所有子目录方法

    Directory.Exist(path); //判断目录是否存在方法

     

  • 相关阅读:
    HTML页面之间跳转传值
    Ajax之三种数据传输格式
    css选择器
    jQuery Validate
    正则表达式
    JSP的九大内置对象,七大动作指令,四个作用域,三个编译指令
    Zooeeper之paxos算法
    ZooKeeper之选举(fastleaderelection算法)
    ZooKeeper之ZAB协议
    ZooKeeper之三阶段提交(3PC)
  • 原文地址:https://www.cnblogs.com/DrHao/p/4900716.html
Copyright © 2020-2023  润新知