StreamWriter用新文本替换行 [英] StreamWriter replace line with a new text

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

问题描述

是否可以用新文本替换文本文件中的文本而不擦除其他数据,这里是我的示例代码,但它不起作用,我知道它有一个问题,但我无法弄清楚,谢谢,

  private void button1_Click_1(object sender,EventArgs e)
{
StreamReader sr = new StreamReader ( test10101.txt);
列表< string> lines = new List< string>();
while(!sr.EndOfStream)
lines.Add(sr.ReadLine());
output = Convert.ToInt32(textBox1.Text);
newbal = Convert.ToInt32(lines [0]) - output;
MessageBox.Show(请拿到您的现金.... \\\
\\\
您的新余额是:$+ newbal);
sr.Close();
{
string linetoreplace = lines [0];
int newlinevalue = newbal;
string contents = sr.ReadToEnd();

StreamWriter sw = new StreamWriter(test10101.txt+.tmp);
// contents = Regex.Replace(contents,linetoreplace,newlinevalue.ToString());
contents = contents.Replace(linetoreplace,newlinevalue.ToString());
sw.WriteLine(contents);
sw.Close();


$ / code>

我想知道是否使用正则表达式或直接替换行,

解决方案

您可以更容易地做到这一点:

  string [] lines = System.IO.File.ReadAllLines(test); 
lines [0] = / *用你需要的任何东西替换* /
System.IO.File.WriteAllLines(test,lines);

希望这可以帮助我

d建议使用 int.TryParse 如果您不希望在您的代码部分中引发异常,以防文件的第一行或文本框值不是数字



如果你真的想使用streamwriter,你可以用这个简单的方法:

  line [0] = newbal.ToString(); 
foreach(字符串s,行)
sw.WriteLine(s);


Is it possible to replace the text in a text file with a new text without erasing the other data, here is my sample code, but its not working, I know there's a problem with it but I can't figure out, thanks,

private void button1_Click_1(object sender, EventArgs e)
{
    StreamReader sr = new StreamReader("test10101.txt");
    List<string> lines = new List<string>();
    while (!sr.EndOfStream)
        lines.Add(sr.ReadLine());
    output = Convert.ToInt32(textBox1.Text);
    newbal = Convert.ToInt32(lines[0]) - output;
    MessageBox.Show("Please get your cash....\n\nYour new balance is: $" + newbal);
    sr.Close();
    {
        string linetoreplace = lines[0];
        int newlinevalue = newbal;
        string contents = sr.ReadToEnd();

        StreamWriter sw = new StreamWriter("test10101.txt" + ".tmp");
        //contents = Regex.Replace(contents, linetoreplace, newlinevalue.ToString());
        contents = contents.Replace(linetoreplace, newlinevalue.ToString());
        sw.WriteLine(contents);
        sw.Close();

    }

I'm wondering if I use the Regex or directly replace the line,

解决方案

You could do it a lot more easily:

        string[] lines = System.IO.File.ReadAllLines("test");
        lines[0] = /* replace with whatever you need */
        System.IO.File.WriteAllLines("test", lines);

hope this helps

also I'd suggest using int.TryParse if you don't want an exception to be raised in your portion of code in case the first line of the file or the textbox values aren't numeric

if you really want to use the streamwriter you could go with this, also a simpler way:

line[0] = newbal.ToString();
foreach(string s in lines)
    sw.WriteLine(s);

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

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