展开全部
新建一个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。这些兄台可以自己看书去学
引入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。这些兄台可以自己看书去学
展开全部
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();
}
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.添加命名空间
System.IO;
System.Text;
2.文件的读取
(1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。
(2).使用StreamReader读取文件,然后一行一行的输出。
3.文件的写入
(1).使用FileStream类创建文件,然后将数据写入到文件里。
(2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。
System.IO;
System.Text;
2.文件的读取
(1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。
(2).使用StreamReader读取文件,然后一行一行的输出。
3.文件的写入
(1).使用FileStream类创建文件,然后将数据写入到文件里。
(2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
引用huntagain2008的回答:
新建一个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。这些兄台可以自己看书去学
新建一个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。这些兄台可以自己看书去学
展开全部
Line没有定义
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询