关于输入输出流IO实现存取数据的问题? 现有一个txt文件,用来存数据,存有一个字符串,如何先读取里面的字符串,然后再将里面的字符串进行修改?听说输入输出流可以实现,没接触过,哪位高手愿意帮忙写一下的?项目是由C#做的! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 File.ReadAllTextFile.WriteAllTextStreamReader 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(); 引入命名空间,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(); 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()); } } }} 我如果做成安装的话,路径"c:\\a.txt" 能否做成相对路径啊 直接把路径写成"a.txt"就是程序运行目录下的a文件,但是如果做成相对路径的话,有时会出现很多问题,所以尽量写绝对路径比较好一点,可以先获得程序运行目录string dir=System.IO.Directory.GetCurrentDirectory();然后补全完整路径,比如程序目录A文件夹下的a.txt就可以写成dir+"\\A\\a.txt",没有必要非强行写成相对路径。个人认为没太大的好处。 对相对路径我没什么好感,前段时间做了一个操作XML的项目,最开始写的全是相对路径,后来测试的时候发现因为读写XML比较频繁,经常出现找不到某个文件的情况,后来改成绝对路径就没有问题了。 Unicode 编码中2460-24FF段内包含封闭式字母数字 将输入字符串中段内封闭式字母数字为普通数字字母 WCF事件回调的问题。 导入Excel文件后,程序的运行位置变了 C# 线程里面 传参怎么传啊? jmail4.4pro接收邮件,标题出现乱码,内容却正常显示(标题和内容都是utf8编码) .net 2.0 环境 怎么判断客户端ip类别 .cs文件如何在VS.NET中打开 Serializable是否不能系列化包含Hashtable属性的类 问:DataTableMapping的构造函数的参数 有关搜索的问题? C#正则表达式提取数据 求一个类似google输入提示的控件!
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();
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();
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());
}
}
}
}