现有一个txt文件,用来存数据,存有一个字符串,如何先读取里面的字符串,然后再将里面的字符串进行修改?听说输入输出流可以实现,没接触过,哪位高手愿意帮忙写一下的?项目是由C#做的!

解决方案 »

  1.   

    File.ReadAllText
    File.WriteAllText
    StreamReader sr = new StreamReader("c:\\a.txt");
                string content = sr.ReadToEnd();
                sr.Close(); 
                StreamWriter sw = new StreamWriter("c:\\a.txt", false, Encoding.Unicode);
                sw.Write("");
                sw.Close();
      

  2.   

    引入命名空间,using System.IO;
    FileStream fs=new FileStream(文件完整路径,例如  @“d:\a.txt”,FileMode.Open);
    StreamReader sr=new StreamReader(fs);
    string content=sr.ReadToEnd();//读取文件内容,content为读到的字符串
    sr.Close();
    fs.Close();//修改字符串的代码略//写入文件
    StreamWriter sw=new Streamwriter(文件路径);
    sw.Write(content);
    sw.Close();
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;namespace notepad
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private string ofd_filepath = string.Empty;
            private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "文本文件(*.txt)|*.txt";
                ofd.Title = "打开文本文件";
                ofd.Multiselect = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    ofd_filepath = Path.GetFullPath(ofd.FileName);
                    richTextBox1.Text = string.Empty;
                    using (StreamReader sr = new StreamReader(ofd.FileName, Encoding.Default))
                    {
                        while (sr.Peek() > -1)
                        {
                            richTextBox1.Text += sr.ReadLine();
                        }
                        sr.Close();
                    }
                }
            }        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                try
                {
                    FileStream fs = new FileStream(ofd_filepath, FileMode.OpenOrCreate, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                    sw.BaseStream.Seek(0,SeekOrigin.Begin);
                    sw.Write(richTextBox1.Text);
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                    MessageBox.Show("保存成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("保存失败!错误信息:" + ex.ToString());
                }
            }
        }
    }
      

  4.   

    我如果做成安装的话,路径"c:\\a.txt"  能否做成相对路径啊
      

  5.   

    直接把路径写成"a.txt"就是程序运行目录下的a文件,但是如果做成相对路径的话,有时会出现很多问题,所以尽量写绝对路径比较好一点,可以先获得程序运行目录string dir=System.IO.Directory.GetCurrentDirectory();然后补全完整路径,比如程序目录A文件夹下的a.txt就可以写成dir+"\\A\\a.txt",没有必要非强行写成相对路径。个人认为没太大的好处。
      

  6.   

    对相对路径我没什么好感,前段时间做了一个操作XML的项目,最开始写的全是相对路径,后来测试的时候发现因为读写XML比较频繁,经常出现找不到某个文件的情况,后来改成绝对路径就没有问题了。