📜  C#中文件处理的基础

📅  最后修改于: 2021-05-29 22:38:04             🧑  作者: Mango

通常,该文件用于存储数据。术语“文件处理”指的是各种操作,例如创建文件,从文件读取,写入文件,附加文件等。在文件处理中最常使用的两个基本操作是文件的读取和写入。当我们打开文件进行读写时,文件变为流。流是用于通信的字节序列。从文件可以形成两个流,一个是输入流,用于读取文件,另一个是输出流,用于写入文件。在C#中, System.IO命名空间包含处理输入和输出流并提供有关文件和目录结构信息的类。

文件处理类层次结构

在这里,我们将讨论两个类,这两个类对于写入和读取文本文件很有用。

StreamWriter类

StreamWriter类实现了TextWriter,用于编写要以特定格式流式传输的字符。该类包含以下常用的方法。

Method Description
Close() Closes the current StreamWriter object and stream associate with it.
Flush() Clears all the data from the buffer and write it in the stream associate with it.
Write() Write data to the stream. It has different overloads for different data types to write in stream.
WriteLine() It is same as Write() but it adds the newline character at the end of the data.

例子:

// C# program to write user input 
// to a file using StreamWriter Class
using System;
using System.IO;
  
namespace GeeksforGeeks {
      
class GFG {
      
    class WriteToFile {
          
        public void Data()
        {
            // This will create a file named sample.txt
            // at the specified location 
            StreamWriter sw = new StreamWriter("H://geeksforgeeks.txt");
              
            // To write on the console screen
            Console.WriteLine("Enter the Text that you want to write on File"); 
              
            // To read the input from the user
            string str = Console.ReadLine(); 
              
            // To write a line in buffer
            sw.WriteLine(str); 
              
            // To write in output stream
            sw.Flush(); 
              
            // To close the stream
            sw.Close(); 
        }
    }
      
    // Main Method
    static void Main(string[] args)
    {
        WriteToFile wr = new WriteToFile();
        wr.Data();
        Console.ReadKey();
    }
}
}

输入:

输出:您将在指定位置找到包含以下内容的文件:

StreamReader类

StreamReader类实现TextReader,以特定格式从流中读取字符。该类包含以下常用的方法。

Method Description
Close() Closes the current StreamReader object and stream associate with it.
Peek() Returns the next available character but does not consume it.
Read() Reads the next character in input stream and increment characters position by one in the stream
ReadLine() Reads a line from the input stream and return the data in form of string
Seek() It is use to read/write at the specific location from a file

例子:

// C# program to read from a file
// using StreamReader Class
using System;
using System.IO;
  
namespace GeeksforGeeks {
      
class GFG {
      
    class ReadFile {
          
        public void DataReading()
        {
            // Takinga a new input stream i.e. 
            // geeksforgeeks.txt and opens it
            StreamReader sr = new StreamReader("H://geeksforgeeks.txt");
              
            Console.WriteLine("Content of the File"); 
              
            // This is use to specify from where 
            // to start reading input stream
            sr.BaseStream.Seek(0, SeekOrigin.Begin);
              
            // To read line from input stream
            string str = sr.ReadLine(); 
              
            // To read the whole file line by line
            while (str != null) 
            {
                Console.WriteLine(str);
                str = sr.ReadLine();
            }
            Console.ReadLine(); 
              
            // to close the stream
            sr.Close(); 
        }
    }
      
    // Main Method
    static void Main(string[] args)
    {
        ReadFile wr = new ReadFile();
        wr.DataReading();
    }
}
}

输出: