如何插入行中的文本文件的第一行? [英] how to insert row in first line of text file?

查看:155
本文介绍了如何插入行中的文本文件的第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含

1,2,3
2,3,4
5,6,7

我要插入的第一行是: A,b,C

I want to insert this into the first line: A,B,C

所以,我得到的:

A,B,C
1,2,3
2,3,4
5,6,7

我怎样才能做到这一点?

How can I do this?

推荐答案

类似以前的答案,但是这说明了如何做你想做的,同时减少内存消耗做什么。有没有办法通过你要修改的整个文件读取左右,即使你在读/写操作流打开它,因为你不能插入数据。

Similar to the previous answers, but this illustrates how to do what you want to do while minimizing memory consumption. There is no way around reading through the entire file you want to modify, even if you open it in a read/write stream, because you can't "insert" data.

static void WriteABC(string filename)
{
    string tempfile = Path.GetTempFileName();
    using (var writer = new StreamWriter(tempfile))
    using (var reader = new StreamReader(filename))
    {
        writer.WriteLine("A,B,C");
        while (!reader.EndOfStream)
            writer.WriteLine(reader.ReadLine());
    }
    File.Copy(tempfile, filename, true);
}

这篇关于如何插入行中的文本文件的第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆