c#如何读写文本文件

能给个范例代码吗?最简单,但完整的。

新建一个Log.txt文件
引入System.IO名称空间,用文件流
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StreamWrite
{
class Program
{
static void Main(string[] args)
{
try
{
FileStream aFile = new FileStream("Log.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);

bool truth = true;
// Write data to file.
sw.WriteLine("Hello to you.");
sw.WriteLine("It is now {0} and things are looking good.",
DateTime.Now.ToLongDateString());
sw.Write("More than that,");
sw.Write(" it's {0} that C# is fun.", truth);
sw.Close();
}
catch (IOException ex)
{
Console.WriteLine("An IOException has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return;
}
}
}
}

读取文件,这里介绍StreamReader对象
static void Main(string[] args)
{
string strLine;
try
{
FileStream aFile = new FileStream("Log.txt",FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
//Read data in line by line 这个兄台看的懂吧~一行一行的读取
while(strLine != null)
{
Console.WriteLine(strLine);
Line = sr.ReadLine();
}
sr.Close();
}catch (IOException ex)
{
Console.WriteLine("An IOException has been thrown!");
Console.WriteLine(ex.ToString());
Console.ReadLine();
return;
}
}
另外对于简单的文档可以直接sr.ReadToEnd()从头读到尾,还有sr.Read() 返回类型char。这些兄台可以自己看书去学
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-05-21
C#读取文本文件代码事例:

try
{
FileStream fs = new FileStream("C:\\aaa.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamReader m_streamReader = new StreamReader(fs);

//用StreamReader类来读取文件
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

//从数据流中读取每一行,只到文件的最后一行
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
// 这里处理每一行
strLine = m_streamReader.ReadLine();
}
m_streamReader.Close();
}
catch (Exception ee)
{
MessageBox.Show("错误:" + ee.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

写文件

FileStream fs = new FileStream("aaa.txt", FileMode.Create, FileAccess.Write);
try
{
using (StreamWriter m_streamWriter = new StreamWriter(fs))
{
m_streamWriter.Flush();
m_streamWriter.WriteLine("你要写的内容");
m_streamWriter.Flush();
m_streamWriter.Dispose();
m_streamWriter.Close();
}
}
catch (Exception ee)
{
Console.WriteLine(ee.Message);
}
finally
{
fs.Dispose();
fs.Close();
}
第2个回答  2012-05-21
1.将textbox中的写进txt文件中
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Text != "")
{
string File_path = @"d:\Test_File\aaaaa.xls";
FileStream fs1 = new FileStream(File_path, FileMode.Append, FileAccess.Write);
StreamWriter sw1 = new StreamWriter(fs1, Encoding.UTF8);
sw1.WriteLine(textBox1.Text.Trim() + " " + DateTime.Now.ToLongTimeString());
sw1.Close();
fs1.Close();
}
}
}
2.将txt中的内容读出,在textbox中显示
private void button_Display_Click(object sender, EventArgs e)
{
string File_path = @"d:\Test_File\aaaaa.xls";
StreamReader sr = File.OpenText(File_path);
textBox2.Text = sr.ReadToEnd();
sr.Close();
textBox2.WordWrap = true;

}
第3个回答  2015-10-31
1.添加命名空间
System.IO;

System.Text;

2.文件的读取

(1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。

(2).使用StreamReader读取文件,然后一行一行的输出。
3.文件的写入
(1).使用FileStream类创建文件,然后将数据写入到文件里。
(2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。
第4个回答  2018-04-15
Line没有定义

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网