用行将新行写入文本文件 [英] Write new lines into text file with lines

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

问题描述

你好,
我有写行的文本文件.我要打开它,然后在那几行写新的新行,而不要覆盖旧的行.

我已经使用StreamWriter尝试使用以下代码进行FileStream:

Hello,
I have text file with writen lines. I want to open it and after that lines write new new line and don''t overwrite old lines.

I''ve tried FileStream with StreamWriter with this code:

sw.WriteLine();
sw.WriteLine();
sw.WriteLine();
sw.WriteLine("{0}", value);



但这只会在写入的行之前产生空白行.

请怎么做?谢谢



but this will only make blank lines before writen lines.

Please how to do it? Thanks

推荐答案

前几天,您才问过一个类似的问题-确实,您应该适当地研究这个主题,而不要继续提出问题.但是,这次我会再次回答您的问题...

由于打开了文件流,并且流通过指向文件的开头而开始,因此调用WriteLine()方法将一直在开头添加内容.在操作文件时(特别是当它只是一个文本文件时)保持文件打开通常是一件不好的事.因此,使用StreamReader对象打开文件,并使用ReadToEnd()方法读取该文件中的所有文本.将此存储在变量中的任何地方都没关系.无论如何,请记住在StreamReader对象上调用Close.然后按照我在对上一个问题的回答中向您展示的那样,对字符串进行操作(即添加新行).最后,使用StreamWriterWriteAlltext包含文件文本的变量-(正如上一个问题中也有人指出的那样).切记要使其覆盖现有内容,因为首先您所拥有的只是该文件的副本.还记得在StreamWriter上调用Close以便释放文件.

希望这会有所帮助,
埃德

请参见上一个问题的解决方案3,以获得使用File类的一个好例子-这比StreamWriters/Readers好!
You asked about a similar problem only the other day - really you ought to research this topic properly not keep asking questions on it. However, I will answer your question again this time...

Since you open a stream to the file and the stream starts by pointing to the start of the file, calling the WriteLine() method will just keep adding stuff to the beginning. Keeping a file open while doing manipulation of it (especially when it is just a text file) is normally a bad thing to do. Therefore, use a StreamReader object to open the file and use the ReadToEnd() method to read in all the text from that file. Store this in a variable wherever, it doesn''t matter really. Anyway, remeber to call Close on your StreamReader object. Then manipulate (i.e. add new lines) to the string as I showed you in my answer to your last question. Finally, use a StreamWriter to WriteAlltext your variable containing the text to the file - (As someone also showed in your last question). Remember to make it overwrite existing content since what you had was a copy of the file in the first place. Remember also to call Close on your StreamWriter so that it releases the file.

Hope this helps,
Ed

See Solution 3 of your last question for a good example that uses the File class - this is better than StreamWriters/Readers!


我不确定埃德的答案就足够了.

使用流时,应注意使用现有文件的模式.仅当打开文件或流时,才可以定义是覆盖文件的内容还是附加到文件.

例如,StreamWriter具有不同的构造函数:
http://msdn.microsoft.com/en-us/library/system.io. streamwriter.aspx [^ ].

一些构造函数具有append参数:
http://msdn.microsoft.com/en-us/library/36b035cb.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/0wf7ab94.aspx [ ^ ].

与流和文件类相同.创建实例时,只需注意选项或文件模式"即可.

—SA
I''m not sure the answer by Ed would be enough.

When working with stream, you should pay attention for the mode of working with existing files. Only when you open a file or stream, you can define if you are going to override the content of the file or append to it.

For example StreamWriter has different constructors:
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx[^].

Some constructors have append parameter:
http://msdn.microsoft.com/en-us/library/36b035cb.aspx[^],
http://msdn.microsoft.com/en-us/library/f5f5x7kt.aspx[^],
http://msdn.microsoft.com/en-us/library/0wf7ab94.aspx[^].

Same thing with stream and file classes. Just pay attention for the options or "file mode" when you create an instance.

—SA


这篇关于用行将新行写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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