一、File

1、File为静态类

File类,是一个静态类,支持对文件的基本操作,包括创建,拷贝,移动,删除和打开一个文件。File类方法的参量很多时候都是路径path。主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。

常用的方法

 

二、FileStream

FileStream文件流 只能处理原始字节(raw byte)。FileStream 类可以用于任何数据文件,而不仅仅是文本文件。FileStream 对象可以用于读取诸如图像和声音的文件,FileStream读取出来的是字节数组,然后通过编码转换将字节数组转换成字符串。

string str = "今天天气好晴朗,处处好风光";
            byte[] buttf = Encoding.Default.GetBytes(str);
            //文件流的写入
            using (FileStream fscreat = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                fscreat.Write(buttf, 0, buttf.Length);
            }

三、StreamWriter

StreamWriter 类主要用于向流中写入数据

属性或方法 作用
bool AutoFlush 属性,获取或设置是否自动刷新缓冲区
Encoding Encoding 只读属性,获取当前流中的编码方式
void Close() 关闭流
void Flush() 刷新缓冲区
void Write(char value) 将字符写入流中
void WriteLine(char value) 将字符换行写入流中
Task WriteAsync(char value) 将字符异步写入流中
Task WriteLineAsync(char value)  将字符异步换行写入流中

 

    class Program
    {
        static void Main(string[] args)
        {
            string path = @"D:\\code\\test.txt";
            //创建StreamWriter 类的实例
            StreamWriter streamWriter = new StreamWriter(path);
            //向文件中写入姓名
            streamWriter.WriteLine("小张");
            //向文件中写入手机号
            streamWriter.WriteLine("13112345678");
            //刷新缓存
            streamWriter.Flush();
            //关闭流
            streamWriter.Close();
        }
    }

四、StringWriter

实现用于将信息写入字符串的 TextWriter信息存储在基础 StringBuilder 中。

using System;
using System.IO;

class StringRW
{
    static void Main()
    {
        string textReaderText = "TextReader is the abstract base " +
            "class of StreamReader and StringReader, which read " +
            "characters from streams and strings, respectively.\n\n" +

            "Create an instance of TextReader to open a text file " +
            "for reading a specified range of characters, or to " +
            "create a reader based on an existing stream.\n\n" +

            "You can also use an instance of TextReader to read " +
            "text from a custom backing store using the same " +
            "APIs you would use for a string or a stream.\n\n";

        Console.WriteLine("Original text:\n\n{0}", textReaderText);

        // From textReaderText, create a continuous paragraph 
        // with two spaces between each sentence.
        string aLine, aParagraph = null;
        StringReader strReader = new StringReader(textReaderText);
        while(true)
        {
            aLine = strReader.ReadLine();
            if(aLine != null)
            {
                aParagraph = aParagraph + aLine + " ";
            }
            else
            {
                aParagraph = aParagraph + "\n";
                break;
            }
        }
        Console.WriteLine("Modified text:\n\n{0}", aParagraph);

        // Re-create textReaderText from aParagraph.
        int intCharacter;
        char convertedCharacter;
        StringWriter strWriter = new StringWriter();
        strReader = new StringReader(aParagraph);
        while(true)
        {
            intCharacter = strReader.Read();

            // Check for the end of the string 
            // before converting to a character.
            if(intCharacter == -1) break;

            convertedCharacter = Convert.ToChar(intCharacter);
            if(convertedCharacter == '.')
            {
                strWriter.Write(".\n\n");

                // Bypass the spaces between sentences.
                strReader.Read();
                strReader.Read();
            }
            else
            {
                strWriter.Write(convertedCharacter);
            }
        }
        Console.WriteLine("\nOriginal text:\n\n{0}", 
            strWriter.ToString());
    }
}

 

版权声明:本文为hardenzhao原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hardenzhao/p/12349773.html