zoukankan      html  css  js  c++  java
  • 修改文本文件某一行数据的两种方法

    1)File类

    //找到setLocation
    string[] ary = File.ReadAllLines(path, Encoding.Default);
    ary = ary.Select(t => t.Trim().StartsWith("setLocation") ? setLocation : t.Trim()).ToArray();
    File.WriteAllLines(path, ary, Encoding.Default);

    2)StreamReader类

    string text = "";
    using (StreamReader reader = new StreamReader(path, Encoding.Default))
    {
    string line = reader.ReadLine();
    while (line != null)
    {
    //如果这一行以setLocation开始,就替换原来的信息
    if (line.StartsWith("setLocation"))
    {
    line = setLocation;
    }
    text += line + "
    ";
    line = reader.ReadLine();
    }
    reader.Close();
    reader.Dispose();
    }
    //写入流,更新数据
    using (StreamWriter writer = new StreamWriter(path))
    {
    writer.Write(text);
    writer.Close();
    writer.Dispose();
    }
  • 相关阅读:
    android 显示自定义视图对话框
    android为按钮事件进行监听过程
    实验三
    实验二
    实验一
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/nora/p/8064227.html
Copyright © 2011-2022 走看看