从流写入字符串

本文关键字:字符串 | 更新日期: 2023-09-27 18:17:36

我试图使用流写入器写入临时创建的文件即_logFileName,同时将写入文件的数据写入使用流读取器的字符串。当前代码显示没有错误,但在运行时说它不能从_logFileName中读取,因为它已经在使用。

我该怎么做呢?

        using (StreamWriter _logFile = File.CreateText(_logFileName))
        {
            //string s = "";
            //using (StreamReader fill_log = new StreamReader(s))
            using (StreamReader fill_log = new StreamReader(_logFileName)) 
            {
                _logFile.WriteLine("Logfile name is: " + _logFileName);
                content += fill_log.ReadLine(); 
                _logFile.WriteLine("LOG FILE STARTED AT: " + _startDateTime.ToString());
                content += fill_log.ReadLine(); 
                _logFile.WriteLine("============================================");
                content += fill_log.ReadLine(); 
                _logFile.Write(_message);
                content += fill_log.ReadLine(); 
                _logFile.WriteLine();
                content += fill_log.ReadLine(); 
            }
            _logFile.Close();
        }
因此根据建议,我将代码更改为:
 using (var fsWrite = new FileStream(_logFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
        using (var _logFile = new StreamWriter(fsWrite))
        using (var fsRead = new FileStream(_logFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (var fill_log = new StreamReader(fsRead))
        {
            _logFile.WriteLine();
            content += fill_log.ReadLine();  
            _logFile.WriteLine("TIME OF LOG ENTRY: " + DateTime.Now);
            content += fill_log.ReadLine();
            // Arbitrary objects can also be written to the file.
            _logFile.WriteLine(_message);
            content += fill_log.ReadLine();
            _logFile.Flush();
            _logFile.Close();

这样做,我可以同时写和写!这没有问题。谢谢。但是内容字符串变量似乎在everyright之后结束。你知道为什么会这样吗?

从流写入字符串

为了能够同时读取和写入同一个文件,您必须使用带有FileShare参数的构造函数之一手动创建FileStream对象,例如这个。

using (var fsWrite = new FileStream(name, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
using (var _logFile = new StreamWriter(fsWrite))
using (var fsRead = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var fill_log = new StreamReader(fsRead))
{
    ...
}

实现您想要的另一种方法是使用专门的TextWriter编写StreamWriter和StringBuilder:

    using (StreamWriter _logFile = File.CreateText(_logFileName))
    {
        using (var builder = new StringBuildingStreamWriter(_logFile)) 
        {
            builder.WriteLine("Logfile name is: " + _logFileName);
            builder.WriteLine("LOG FILE STARTED AT: " + _startDateTime.ToString());
            builder.WriteLine("============================================");
            builder.Write(_message);
            builder.WriteLine();
            content += builder.ToString();
        }            
        _logFile.Close();
    }

public class StringBuildingStreamWriter:TextWriter
{
    StringBuilder sb = new StringBuilder();
    private StreamWriter sw;
    public StringBuildingStreamWriter(StreamWriter sw)
    {
        this.sw = sw;
    }
    public override void WriteLine(string value)
    {
        sb.AppendLine(value);
        sw.WriteLine(value);
    }
    public override void WriteLine()
    {
        sw.WriteLine();
        sb.AppendLine();
    }
    public override void Write(string value)
    {
        sb.Append(value);
        sw.Write(value);
    }
    public override string ToString()
    {
        return sb.ToString();
    }
    public override Encoding Encoding
    {
        get { return UTF8Encoding.UTF8; }
    }
}