读写 txt 如题 我想做一个 txt 的 新手 根本以前没接触过! 在网上看的 也没看懂! 都十一了 也不知道怎么做! 大侠 们 十一快乐! 帮个忙! 最好来个注释的! 嘿嘿! 急急啊! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 StreamReader,StreamWriter百度 C# 文本文件 这个偶也没做过上google找的话 应该也不少 写文件 FileStream fs = new FileStream(@"F:/aa.txt", FileMode.OpenOrCreate, FileAccess.Write);//F:/aa.txt为创建文本文件的路径 StreamWriter writer = new StreamWriter(fs); writer.Flush(); writer.BaseStream.Seek(0,SeekOrigin.Begin); writer.Write("这里是内容"); writer.Flush(); writer.Close(); 读文件:FileStream fs = new FileStream(@"F:/aa.txt", FileMode.Open, FileAccess.Read); StreamReader reader = new StreamReader(fs,System.Text.Encoding.GetEncoding("UTF-8")); reader.BaseStream.Seek(0, SeekOrigin.Begin); string content = ""; string strLine = reader.ReadLine(); while (strLine != null) { content += strLine+"\n"; strLine = reader.ReadLine(); } reader.Close(); txtContent.Text = content;//用来接收读取文件的变量 此处为bs的一文本控件 private string readstr {//读 string path = @"C:\test.txt";//自己改路径,可以写成参数传进来 string url = null; try { if (!File.Exists(path)) { MessageBox.Show("no files!!"); return null; } else { int number; byte[] bb = new byte[100]; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); while ((number = fs.Read(bb, 0, 100)) != 0) { url = url + (new UTF8Encoding(true).GetString(bb)); bb = new byte[100]; } return url; } } catch { return null; } }private void writestr(string url)//写 { string path = @"C;\test.txt";//自己改路径,可以写成参数传进来 try { if (File.Exists(path)) File.Delete(path); FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); byte[] urlbyte = new UTF8Encoding(true).GetBytes(url); fs.Write(urlbyte, 0, urlbyte.Length); fs.Flush(); } catch { } } 本帖最后由 net_lover 于 2011-09-30 13:10:33 编辑 求一个过滤pre标签的正则 用vs2010做的asp.net程序怎么发布到公司远程服务器上去啊 向net_lover提示:按你的代码写怎么达不到预期? 把txt文件中数据导入到数据库? 下面代码是什么意思啊 这样的需求如何实现? 如何给ID加密啊~~!!! 再问水晶报表的登陆,解决立刻给分 高分寻求支持ASP.NET 1.1的免费空间 怎样限制用户只能在某一单元格(如B11)输入,而不能编辑其它单元格的内容? 一个怪异的问题 【online】定义变量时,都定义成 var 格式,好还是不好啊?
上google找的话 应该也不少
FileStream fs = new FileStream(@"F:/aa.txt", FileMode.OpenOrCreate, FileAccess.Write);//F:/aa.txt为创建文本文件的路径
StreamWriter writer = new StreamWriter(fs);
writer.Flush();
writer.BaseStream.Seek(0,SeekOrigin.Begin);
writer.Write("这里是内容");
writer.Flush();
writer.Close();
FileStream fs = new FileStream(@"F:/aa.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(fs,System.Text.Encoding.GetEncoding("UTF-8"));
reader.BaseStream.Seek(0, SeekOrigin.Begin);
string content = "";
string strLine = reader.ReadLine();
while (strLine != null)
{
content += strLine+"\n";
strLine = reader.ReadLine();
}
reader.Close();
txtContent.Text = content;//用来接收读取文件的变量 此处为bs的一文本控件
private string readstr {//读
string path = @"C:\test.txt";//自己改路径,可以写成参数传进来
string url = null;
try
{
if (!File.Exists(path))
{
MessageBox.Show("no files!!");
return null;
}
else
{
int number;
byte[] bb = new byte[100];
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
while ((number = fs.Read(bb, 0, 100)) != 0)
{
url = url + (new UTF8Encoding(true).GetString(bb));
bb = new byte[100]; }
return url;
} }
catch
{
return null;
}
}
private void writestr(string url)//写 {
string path = @"C;\test.txt";//自己改路径,可以写成参数传进来
try
{
if (File.Exists(path))
File.Delete(path);
FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
byte[] urlbyte = new UTF8Encoding(true).GetBytes(url);
fs.Write(urlbyte, 0, urlbyte.Length);
fs.Flush();
}
catch
{ }
}