IO Operation

io操作中常使用的类包括有FileStream,File,Directory,Path,FileInfo, DirectoryInfo,FileStreamInnfo,StreamReader,StreamWriter,FileSystemWatcher,上面的类存在于System.IO命名空间中,另外在System.IO.Compression命名空间中还存在下面的两个类,DeflateStream和GZipStream,这两个类分别允许使用Deflate和gzip模式来读写压缩文件。

1.File and Directory class

方法说明
Copy将文件复制到目的地址
Create用于在规定的路径创建一个文件
Delete删除文件
Open打开文件,返回的是FileStream
Move实现的是文件的转移

需要注意的是File市System.IO中的静态成员,所以在使用是不能直接new,而是直接使用本身

File.Open (@”PATH”)

方法说明
CreateDirectory创建指定的路径
Delete删除。。
GetDirectories放回当前目录名
GetFiles得到当前目录下的文件名的string数组
GetFilesSystemEntries除上面的文件名之外还包括的有目录名
Move将指定的目录移动到其他的位置
  

Directory同样是static class,使用同上

2.FileInfo class

属性说明
Directory返回的是DirectoryInfo
DirectoryName文件目录名
IsReadOnly文件的属性read only
Length文件的长度

FileInfo fileInfo = new FileInfo(@"c:/test.txt");创建FileInfo对象,然后使用上面的属性

3.DirectoryInfo class

属性说明
Parent返回DirectoryInfo对象
Root当前目录的根目录(例如c:/)
此处需要注意的是相对路径和绝对路径,绝对路径没什么可讲的,但是相对路径的话,在vs2008中启动debug一个程序的话,当前路径指的是ProgramName/bin/debug,如果需要可以使用Directory.GetCurrentDirectory()找出当前的工作目录,或者使用Directory.SetCurrentDirectory来设置当前目录。例如

Console.WriteLine ( Directory.GetCurrentDirectory() );

4.FileStream class

成员说明
Append追加
Create如果文件存在,首先删除
CreateNew如果文件存在,exception
Open打开现有文件
OpenOrCreate打开或者是创建
Truncate文件截断为0
Seek
文件指针文职移动
Read读取数据
public override int Read(byte[] array, int offset, int count);该函数读取的是byte[]类型的数据,所以需要decode
Write向文件中写入数据,基本的思路char to byte,然后调用Write Method来实现将string类型写入

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IOTest
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] byData = new byte[100];
            char[] chData = new char[100];

            // make the FileStream
            FileStream stream =
                new FileStream(Directory.GetCurrentDirectory() + @"/a.txt", FileMode.Open);
            // read the date from the file
            stream.Read(byData, 0, 100);

            // decode the byte array
            Decoder decoder = Encoding.UTF8.GetDecoder();
            decoder.GetChars(byData, 0, byData.Length, chData, 0);

            // display the message
            Console.WriteLine(chData);

            Console.ReadKey();
        }
    }
}

// write a message to a text file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IOTest
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] msg = ("hello world").ToCharArray();
            byte[] byMsg = new byte[msg.Length];

            Encoder encoder = Encoding.UTF8.GetEncoder();
            int charsUsed, bytesUsed;
            bool completed;
            encoder.GetBytes(msg, 0, msg.Length, byMsg, 0, true);
            FileStream stream = new FileStream(@"./a.txt", FileMode.Create);
            stream.Write(byMsg, 0, byMsg.Length);

            Console.ReadKey();
        }
    }
}

 

 

5.StreamWrite class writes string to file without converting

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IOTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "hello c#";
            FileStream stream = new FileStream(@"./a.txt", FileMode.Create);
            StreamWriter writer = new StreamWriter(stream);

            writer.Write(msg);
            writer.Flush();

            Console.ReadKey();
        }
    }
}

StreamWriter负责将数据传递给FileStram class。大大简化了IO操作

6.StreamReader

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace IOTest
{
    class Program
    {
        static void Main(string[] args)
        {
            FileStream stream = new FileStream(@"./a.txt", FileMode.Open);
            StreamReader reader = new StreamReader(stream);
            string msg = reader.ReadLine();
            Console.WriteLine(msg);

            Console.ReadKey();
        }
    }
}

未完,待续…

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值