美文网首页
文本读写

文本读写

作者: 晓龙酱 | 来源:发表于2017-09-18 10:31 被阅读6次

使用FileStream读取

using(FileStream fs = File.OpenRead(@"/Users/wxl/Workplace/test.txt"))
{
    byte[] arr = new byte[100];
    UTF8Encoding encoding = new UTF8Encoding(true);
    while(fs.Read(arr, 0, arr.Length) > 0)
    {
        Console.WriteLine(encoding.GetString(arr));
    }
}

使用FileStream写入

using(StreamWriter ws = new StreamWriter(@"/Users/wxl/Workplace/test.txt"))
{
    string[] arr = new string[]{"hello\n","汪晓龙"};

    foreach(var str in arr)
    {
        ws.WriteLine(str);
    }
}

使用StreamReader读取

using(StreamReader sr = new StreamReader(@"/Users/wxl/Workplace/test.txt"))
{
    string line;
    while((line = sr.ReadLine()) != null)
    {
        Console.WriteLine(line);
    }
}

使用StreamWriter写入

using(StreamWriter ws = new StreamWriter(@"/Users/wxl/Workplace/test.txt"))
{
    string[] arr = new string[]{"hello","world"};

    foreach(var str in arr)
    {
        ws.WriteLine(str);
    }
}

相关文章

  • 文件和io(cookbook笔记)

    读写文本数据 读写文本数据 读写各种不同编码的文本数据 使用带有 rt 模式的 open() 函数读取文本文件 写...

  • 文本读写

    使用FileStream读取 使用FileStream写入 使用StreamReader读取 使用StreamWr...

  • 2.2、Python进阶02 文本文件的输入输出

    Python具有基本的文本文件读写功能。Python的标准库提供了更丰富的读写功能。文本文件的读写主要通过open...

  • c++ 读写文件

    写文件文本 读文件文本 读写二进制文件

  • Java IO流之拷贝(复制)文件

    方式一(字符流读写复制文件,仅限文本文件) 方式二(字符流缓冲区读写文件-高效,仅限文本文件) 方式三(字节流读写...

  • Python文本文件的输入输出操作学习

    Python具有基本的文本文件读写功能。Python的标准库提供有更丰富的读写功能。 文本文件的读写主要通过ope...

  • python cookbook第五、七章笔记

    5.1 读写文本数据 你需要读写各种不同编码的文本数据,比如ASCII,UTF-8或UTF-16编码等。 使用带有...

  • nodejs读写文本

    fs模块 fs 模块提供了一个 API,用于以模仿标准 POSIX 函数的方式与文件系统进行交互。 使用该模块: ...

  • Python读写文本

    Python 以只读方式打开一段文本 read方法读取整个文本,返回一个string readlines读取文本的...

  • 文件文本读写

    1. open() 函数中的mode模式 2. 文件的读写和访问 3. 文件的关闭 作用:终止对外部文件的连接,同...

网友评论

      本文标题:文本读写

      本文链接:https://www.haomeiwen.com/subject/lcbvsxtx.html