1、首先命名空间不要忘添加using System.IO;

System.IO命名空间提供了许多文件读写操作类,常见的对文件进行操作的方式有两种:文本模式及二进制模式。文本模式常用StreamWriter类来处理。

 

文件头格式:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
2、使用FileStream读写文件
读文件核心代码:
 
byte[] byData = new byte[100];
char[] charData = new char[1000];
try
{
FileStream sFile = new FileStream("文件路径",FileMode.Open);
//例:要读取桌面上的文件,
则“文件路径”=“C:\\Documents and Settings\\hax1sz\\桌面\\AA.txt”
sFile.Seek(55, SeekOrigin.Begin);
sFile.Read(byData, 0, 100);
//第一个参数“ byDat ”是被传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
}
catch (IOException e)
{
Console.WriteLine("An IO exception has been thrown!");
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
Decoder d = Encoding.UTF8.GetDecoder();
d.GetChars(byData, 0, byData.Length, charData, 0);
Console.WriteLine(charData);
Console.ReadLine();
 
写文件核心代码:
 
FileStream fs = new FileStream(文件路径,FileMode.Create);
//获得字节 数组
byte [] data =new UTF8Encoding().GetBytes(String);
//开始写入
fs.Write(data,0,data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
 
3、使用StreamReader和StreamWriter
 
StreamReader读取文件:
 
StreamReader objReader = new StreamReader(文件路径);
      string sLine="";
      ArrayList LineList = new ArrayList();   
      while (sLine != null)
      {
        sLine = objReader.ReadLine();
        if (sLine != null&&!sLine.Equals(""))
          LineList.Add(sLine);
      }
            objReader.Close();
            return LineList;
 
  StreamWriter写文件:
   StreamWriter类实现一个TextWriter,使其以一种特定的编码向流中写入字符。StreamWriter旨在以一种特定的编码输出字符,而从Stream派生的类则用于字节的输入和输出。StreamWriter默认使用UTF8Encoding的实例,除非指定了其他编码。UTF8Encoding的这个实例以UTF-8格式编写的Unicode字节顺序标记,UTF-8可以正确处理所有的Unicode字符并在操作系统的本地化版本上产生一致的结果。

  FileStream fs = new FileStream(文件路径, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write(String);
 //清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
当然我们可以将其写成两个自定义方法,其中Server.MapPath是AA.txt所在的路径。

public void CreateTextWriteText(string s) //向文本文件AA.txt中写入参数s字符串 
        { 
            StreamWriter sw = File.CreateText(Server.MapPath("AA.txt")); 
            sw.WriteLine(s); 
            sw.Close(); 
        }

用来实现向指定的文本文件中追加字符串,其中参数s是向文本文件AA.txt中写入的字符串,File. AppendText方法为创建一个StreamWriter,它将文本追加到现有文件。代码如下:

public void AppendTextWriteText(string s)//向现有文件AA.txt中追加写入字符串s 
        { 
            StreamWriter sw = File. AppendText(Server.MapPath("AA.txt")); 
            sw.WriteLine(s); 
            sw.Close(); 
        }


4、用C#读取.txt文件,常用 StreamReader 类
StreamReader sr = new StreamReader("TestFile.txt")///StreamReader sr = new StreamReader("TestFile.txt",Encoding.GetEncoding("GB2312"))
///GBK
String line;
while ((line = sr.ReadLine()) != null)
{
   textBox1 .Text +=ii.ToString ()+" -"+line.ToString()+"\r\n";

}
加入引用:System.IO
StreamReader objReader = new StreamReader("c:\\test.txt");
     System.IO 命名空间中的对象,尤其是 System.IO.StreamReader 类。

\r\n一般一起用,用来表示键盘上的回车键.也可只用\n\t表示键盘上的“TAB”键。

原网址:http://www.soasp.net/FilePage/200803/20080304222633.aspx

       http://www.csharpwin.com/csharpspace/8967r3113.shtml