数据访问之文件系统数据

一:读文件(利用FileStream对象)

 

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

namespace ReadFile
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] byteData = new byte[200];
            char[] charData = new char[200];
            try
            {
                string fileName = "D:\\2.txt";
                FileStream aFile = new FileStream(fileName, FileMode.Open);
                aFile.Seek(113, SeekOrigin.Begin);
                aFile.Read(byteData, 0, 200);

            }
            catch (IOException e)
            {
         
                Console.WriteLine("A IO Exception has been throw!");
                Console.WriteLine("{0}",e.ToString());
                Console.ReadKey();
                return;
            }
            Decoder decoder = Encoding.UTF8.GetDecoder();
            decoder.GetChars(byteData,0,byteData.Length,charData,0);
            Console.WriteLine("{0}",charData);
            Console.ReadKey();
        }
    }
}

二:写文件(利用FileStream对象)

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

namespace WriteFile
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[] byteData;
            char[] charData;
            try
            {
                string fileName = "D:\\3.txt";
                FileStream aFile = new FileStream(fileName, FileMode.Create);
                charData = "HelloWorld".ToCharArray();
                byteData = new byte[charData.Length];
                //将字符数组转换为字节数组
                Encoder e = Encoding.UTF8.GetEncoder();
                e.GetBytes(charData, 0, charData.Length, byteData, 0, true);
                aFile.Seek(0, SeekOrigin.Begin);
                aFile.Write(byteData, 0, byteData.Length);

            }
            catch (IOException e)
            {

                Console.WriteLine("A IO Exception has been throw!");
                Console.WriteLine("{0}", e.ToString());
                Console.ReadKey();
                return;
            }

        }

 

三:利用StreamWriter对象写文件

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


namespace StreamWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FileStream aFile = new FileStream("D:\\Log.txt", FileMode.OpenOrCreate);
                StreamWriter sw = new StreamWriter(aFile);
                bool truth = true;
                sw.WriteLine("Hello!Log!"); 利用WriteLine()写一行字符串
                sw.WriteLine("It is now {0} and things are longking good.", DateTime.Now.ToLongDateString());
                sw.WriteLine("More than that,");
                sw.Write("it's {0} that c# is fun.", truth);
                sw.Close();
            }
            catch (IOException e)
            {

                Console.WriteLine("A IO Exception has been throw!");
                Console.WriteLine("{0}", e.ToString());
                Console.ReadKey();
                return;
            }
        }
    }
}

 

    }

}

 

四:读文件(通过streamreader对象)

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

namespace StreamRead
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                String fileName = "D:\\Log.txt";
                FileStream fs = new FileStream(fileName, FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                int a = sr.Read();//Read()是读出int型
                Console.WriteLine("{0}", a);
                string b =sr.ReadLine();//ReadLine()来读出字符串型
                while (b != null)
                {
                    Console.WriteLine("{0}", b);
                    b = sr.ReadLine();
                }

                sr.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine("A error !");
                Console.WriteLine(e.ToString());
              
           
            }
            Console.ReadKey();
          
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值