现在的位置: 首页 > 综合 > 正文

C# File对象

2012年08月23日 ⁄ 综合 ⁄ 共 3698字 ⁄ 字号 评论关闭

    File(文件)对象代表—个文件,并为检测文件是否存在、文件更名和文件删除提供了一些有用的方法。所有这些方法都是静态的,也就是说,不能使用NEW运算符创建File的实例,但可以直接使用File的方法。

if(File.Existas("File.txt"))
    File.Delete("File.txt");

也可以用File对象获得用于读写文件数据的文件流(FileStream)。
FileStream fs = File.OpenRead("File.any");

FileStream fs = File.OpenText("File.txt");

下面是一些有用的File方法
------------------------------------------------
File.FileExists(fileName)
File.Delete(fileName)
File.AppendText(String)
File.Copy(fromFile,toFile)
File.Move(fromFile,toFile)
File.GetExtension(fileName)    //获取文件的扩展名
File.HasExtension(fileName)    //若文件有扩展名,则返回true
-----------------------------------------------------------

    要读一个文本文件,首先使用File对象获取一个StreamReader对象,然后用文本流的话方法读取数据。
    StreamReader ts = File.OpenText("File.txt");
    string s = ts.ReadLine();

    要创建一个文本文件并写入数据,可使用CreateText方法获取—个StreamWrite对象。
    //open  for  writing
    StreamWriter  sw  = File.CreateText("File.txt");
    sw.WriteLine("Hello  file");
    如果向—个已经存在的文件追加数据,可以直接创建—个StreamWrite对象,并将用于追加的那个布尔参数设置为true。
    //append to text file
    StreamWriter sw = new StreamWriter("file.txt",true);

=====================================================
    大量经常发生的异常是在处理文件的输入和输出时引起的。非法文件名、文件不存在、目录不存在、非法文件名参数及文件保护错误等都会引起异常。因此,处理文件输入和输出的最好方法是
将文件处理代码放在try块中,这样可以保证掳获所有可能的错误,避免出现令人为难的致命错误。
各种文件类的方法都在其文档中结出了它们能抛出的异常。只要捕获通用Ex配必叨对象就能保证
捕获所有异常,但是如果需要对不同的异常进行不同的处理,既要对异常分别进行检测。
    例如,可以按照下列方式打开一个文本文件。


try
{
    StreamReader ts = File.OpenText("File.txt");
    string s = ts.ReadLine();
}
catch(Exception e )
{
    console.writeLine(e.Message);
}

    检测文件末尾
    有两种可用的方法能保证不会超过文本文件的末尾:(1)查找空(null)异常,(2)查找数据
流的末尾。当读数据超过了文本文件的末尾时,不会出现错误,也不会抛出文件结束的异常。但是,如果在文件末尾后读人一个字符中,会返回一个空值,可以用它在一个文件读人类中创建一个文件结束函数。
    private StreamReader rf; 
    private bool eof;
    //-----------------------------------
    Public  String  readLine()
    {
        string s = rf.ReadLine();
        if(s==null)
            eof=true;
        return s;
    }

    //-----------------------------------
    public bool fEof()
    {
        return eof;
    }

  另外一种保证读数据不会超过文件末尾的方法是,使用Stream的Peek方法在读数据前先查看一下。Peek方法返回文件中下一个字符的ASCII码,如果没有字符则返回 -1.
    public string read_line()
    {
        string s="";
        if(rf.Peek()>0)
            s = rf.ReadLine();
        else
            eof=true;

        return s;
    }

===================================
csFile.cs 类

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace OperateFile
{
    public class csFile
    {
        private string fileName;
        StreamReader ts;
        StreamWriter ws;
        private bool opened, writeOpened;

        public csFile()
        {
            init();
        }

        public csFile(string file_name)
        {
            fileName = file_name;
            init();
        }

        private void init()
        {
            opened = false;
            writeOpened = false;

        }

        //打开欲读数据的文件
        public bool OpenForRead()
        {
            return OpenForRead(fileName);
        }
        //打开欲读数据的文件
        public bool OpenForRead(string file_name)
        {
            fileName = file_name;
            try
            {
                ts = new StreamReader(fileName);
                opened = true;
            }
            catch(FileNotFoundException e)
            {
                return false;
            }
            return true;
        }

       
       
        //从文件读取数据
        public string readLine()
        {
            return ts.ReadLine();
        }

        //写文件
        public void writeLine(string s)
        {
            ws.WriteLine(s);
        }
        public bool OpenForWrite()
        {
            return OpenForWrite(fileName);
        }
        public bool OpenForWrite(string file_name)
        {
            try
            {
                ws = new StreamWriter(file_name);
                fileName = file_name;
                writeOpened = true;
                return true;
            }
            catch(FileNotFoundException e)
            {
                return false;
            }
        }

    }
}

抱歉!评论已关闭.