以编程方式编写特定行我写哪行 [英] Write particular line programatically which line i write

查看:66
本文介绍了以编程方式编写特定行我写哪行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

现在我有一个问题.在这里,我有一个文本文件.我想根据我的需要写该文件的特定行.尽管上述编写过程中所有其他行均未更改.我一次又一次地进行了上述操作,但仍然需要完成. 这里我不需要替换技术 .欢迎所有答复.



在此先感谢

Dhinesh Kumar.V

Dear All,

Now i have one problem. Here i have one text file. And i wanna write particular line of that file based on my need. While the above writing process all other lines are didnt change. I did the above opereation again and again still my need is completed. Here i dont need REPLACE Technique. All Replies are Welcome.



Thanks in Advance

Dhinesh Kumar.V

推荐答案

此处存在一个主要问题:文本文件没有替换行,或者实际上没有行"的概念.它们都是组成整个文件的字符的集合.其中一些字符标记行尾,但是它们之间没有什么特殊之处-如果替换碰巧是换行符的单个字符,则前一行和后一行将串联在一起.

如果要替换文本文件中一行的文本,则必须读取整个文件,替换文本,然后再次写入整个文件,或者:

1)打开一个新文件
2)阅读并复制到更改的要点
3)读取要更改的数据,然后写入更改的数据
4)读取并复制到文件末尾.
5)关闭两个文件.
6)删除原始文件,然后重命名新文件.
There is a major problem here: text files do not have a replace-line, or indeed any concept of "lines". All they are is a collection of characters which make up the whole file. Some of those characters mark a line end, but there is nothing special about them - if you replace a single character that happens to be a newline character, the line before and the line after will be concatenated together.

If you want to replace the text of a line in the text file, you have to read the whole file, replace the text, and the write the whole file again, or:

1) Open a new file
2) Read and copy up to the point of the change
3) Read the data to be changed and write the changed data instead
4) Read and copy up to the end of file.
5) Close both files.
6) Delete the original, and rename the new file.


如果要处理不是很大的文本文件,有一种简单的方法:

There is an easy way if you want process not very large text file:

RichTextBox textBox = new RichTextBox()
{
    Text = File.ReadAllText(fileName),
    WordWrap = false
};

string firstLine, secondLine;

firstLine = textBox.Lines[0];
secondLine = textBox.Lines[1];

textBox.Lines[0] = secondLine;
tectBox.Lines[1] = firstLine;



毕竟,将textBox.Text写入文件中.

是你问的吗?



After all write the textBox.Text on your file.

Is that what you asked?


这篇关于以编程方式编写特定行我写哪行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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