c#读写文件

FileDo.cs

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

namespace First_Demo
{
    class FileDo
    {
                static string GetDate()
        {
            System.DateTime currentTime = System.DateTime.Now;
            //获取当前日期的前一天转换成ToFileTime
            string strYMD = currentTime.AddDays(-1).ToString("yyyyMMdd");
            return strYMD;
        }
 
        static string GetDirectory()
        {
            //设置目录
            //string CurDir = System.AppDomain.CurrentDomain.BaseDirectory + @"SpiFlashDir";
            string CurDir = System.AppDomain.CurrentDomain.BaseDirectory;
            //判断路径是否存在
            if (!System.IO.Directory.Exists(CurDir))
            {
                System.IO.Directory.CreateDirectory(CurDir);
            }
            return CurDir;
        }
 
        public static void WriteBin(String filePath_in, byte[] data)
        {
            if (data == null)
            {
                System.Console.WriteLine("Error: WriteBin is null!!!");
                return;
            }
            String FilePath = GetDirectory() + "\\" + filePath_in;
            using (FileStream fs = new FileStream(FilePath, FileMode.Truncate/*FileMode.OpenOrCreate*/))
            {
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write(data); //以二进制方式向创建的文件中写入内容               
                bw.Close(); //关闭流
                fs.Close();
            }
        }
 
        public static byte[] ReadBin(String filePath_in)
        {                
            byte[] ret = null;
            try
            {
                // 本函数参考:https://blog.csdn.net/qq_42351033/article/details/88088480
 
                String filePath = GetDirectory() + "\\" + filePath_in;
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    BinaryReader br = new BinaryReader(fs);
                    int Count = (int)fs.Length;
                    ret = br.ReadBytes(Count); //以二进制方式读取文件中的内容  
                    br.Close(); //关闭流
                    fs.Close();
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("Error:The file could not be open:");
                Console.WriteLine(e.Message);
            }
            return ret;
        }
 
        public static String WriteText(string data)
        {
            String FilePath = GetDirectory() + "\\" + "MyFileSend" + GetDate() + ".txt";
 
            System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, false); //文件覆盖方式添加内容
 
            file.Write(data); //保存数据到文件
 
            file.Close();  //关闭文件
 
            file.Dispose(); //释放对象
 
            return FilePath;
        }

        public static String WriteText(String FilePath, string data)
        {
            //String FilePath = GetDirectory() + "\\" + "MyFileSend" + GetDate() + ".txt";

            System.IO.StreamWriter file = new System.IO.StreamWriter(FilePath, false); //文件覆盖方式添加内容

            file.Write(data); //保存数据到文件

            file.Close();  //关闭文件

            file.Dispose(); //释放对象

            return FilePath;
        }
 
        /// <summary>
        /// 获取文件中的数据
        /// </summary>
        /// <param name="args"></param>
        public static string ReadText(String filePath)
        {
            string strData = "";
            try
            {
                string line;
// using语句。
// 定义一个范围,在范围结束时处理对象。 
// 场景: 
// 当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。 
// 要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。
 
                // 创建一个 StreamReader 的实例来读取文件 ,using 语句也能关闭 StreamReader
                using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
                {
                    // 从文件读取并显示行,直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        strData = line;
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return strData;
        }

        /// <summary>
        /// 获取文件中的数据
        /// </summary>
        /// <param name="args"></param>
        public static List<string> ReadTextToList(String filePath)
        {
            List<string> strData = new List<string>();
            try
            {
                string line;
                // using语句。
                // 定义一个范围,在范围结束时处理对象。 
                // 场景: 
                // 当在某个代码段中使用了类的实例,而希望无论因为什么原因,只要离开了这个代码段就自动调用这个类实例的Dispose。 
                // 要达到这样的目的,用try...catch来捕捉异常也是可以的,但用using也很方便。

                // 创建一个 StreamReader 的实例来读取文件 ,using 语句也能关闭 StreamReader
                using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
                {
                    // 从文件读取并显示行,直到文件的末尾 
                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                        strData.Add(line);//strData = line;
                    }
                }
            }
            catch (Exception e)
            {
                // 向用户显示出错消息
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            return strData;
        }

    }
}

Program.cs

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

namespace helloworld
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("*********************\n");

            String filePath = FileDo.WriteText("你好。====================="); 
            Console.WriteLine(filePath); 

            FileDo.ReadText(filePath);

            int a = 0x01;
            int b = 0x02; // '\u0002';
            int c = a | b;


            while (true)
            {
            }
        }
    }
}

 

posted on 2019-10-09 23:56  lizhuohui  阅读(49)  评论(0编辑  收藏  举报

导航