• 文件操作FileStream,Log


    1、关于读写文件,犯的一个低级错误,平常代码拷贝习惯了,就像电脑用多了会提笔忘字一样,所以平常还是要多多用心才好。

        这段代码的意图是在文件中写入数据,如果原文件不存在,则先新建。

        事实上,当真的执行了System.IO.File.Create(filename); 再执行System.IO.StreamWriter sr=new System.IO.StreamWriter(filename,true)时会报错:

        The process cannot access the file 'C:Documentsigtext1.txt' because it is being used by another process.

       原因:System.IO.File.Create(filename) 返回值是FileStream,执行了新建但是并没有关闭FileStream,所以文件used by another process.

            private static void write()
            {
                try
                {
                    string filename = @"C:Documentsigtext1.txt";
                    if (!System.IO.File.Exists(filename))
                    {
                        System.IO.File.Create(filename);
                    }
                    using (System.IO.StreamWriter sr=new System.IO.StreamWriter(filename,true))
                    {
                        sr.WriteLine("test12");
                    }
                }
                catch
                {
    
                }
            }

    修改如下即可

            private static void write()
            {
                try
                {
                    string filename = @"C:Usersxiaochun-zhaiDocumentsigtext1.txt";
                    if (!System.IO.File.Exists(filename))
                    {
                       System.IO.FileStream sr= System.IO.File.Create(filename);
                       sr.Close();
                    }
                    using (System.IO.StreamWriter sr=new System.IO.StreamWriter(filename,true))
                    {
                        sr.WriteLine("test12");
                    }
                }
                catch
                {
    
                }
            }

    2、事实上一句代码就可代替上面的方法,以上方法仅仅为了说明System.IO.File.Create使用时要注意的问题。

    string filename = @"C:Documentsigtext1.txt";
    System.IO.File.AppendAllText(filename, "ceshi ");

     3、一个极简单的写日志的类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace writefile
    {
        public class WriteLog
        {
            public static string LogBasePath = @"C:";
            private string logFileName = "Log.txt";
           
            private static object _locker = new object();
            private static WriteLog _instance;
            private WriteLog()
            {
            }
    
            public static WriteLog Instance
            {
                get
                {
                    if (_instance == null)
                    {
                        lock (_locker)
                        {
                            if (_instance == null)
                            {
                                _instance = new WriteLog();
                            }
                        }
                    }
                    return _instance;
                }
            }
            public void LogMessage(string message)
            {
                lock (_locker)
                {
                    string fullPath = System.IO.Path.Combine(WriteLog.LogBasePath, logFileName);
                    System.IO.File.AppendAllText(fullPath, message + Environment.NewLine);
                }
            } 
        }
    }

    调用: WriteLog.Instance.LogMessage("test");

    说明:Environment.NewLine 代表换行

  • 相关阅读:
    备份恢复八大核心
    ORACLE CentOS5.6安装
    ORA-00205
    sf02_选择排序算法Java与Python实现
    解决SQL命令行回退的问题
    redhat 6.4 yum 本地配置简记
    ORA-12705: Cannot access NLS data files or invalid environment specified
    asmca无法创建ASM磁盘
    Sort List
    Merge k Sorted Lists
  • 原文地址:https://www.cnblogs.com/xiaochun126/p/5088362.html
Copyright © 2020-2023  润新知