首页 > 其他分享 >读写文本文件

读写文本文件

时间:2023-10-24 13:23:49浏览次数:30  
标签:string temp FileStream 读写 文本文件 new txt StreamReader

#region 读取文件(内容不多时,一次将文本内容全部读完)
string str1 = System.IO.File.ReadAllText(@"c:\temp\ascii.txt");//返回一个包含全部文本内容的字符串
string str2 = System.IO.File.ReadAllText(@"c:\temp\ascii.txt", Encoding.ASCII);//返回一个包含全部文本内容的字符串
string[] strs = System.IO.File.ReadAllLines(@"c:\temp\ascii.txt"); //返回一个字符串数组,每一行都是一个数组元素
string[] strs2 = System.IO.File.ReadAllLines(@"c:\temp\ascii.txt", Encoding.ASCII);//返回一个字符串数组,每一行都是一个数组元素
#endregion

#region 读取文件(内容较多时)
static StreamReader sr1 = new StreamReader(@"c:\temp\utf-8.txt"); //1.使用StreamReader(FilePath)
static StreamReader sr2 = new StreamReader(@"c:\temp\utf-8.txt", Encoding.UTF8);//2.使用StreamReader(FilePath, Encoding),并指定编码方式 

static FileStream myfs = new FileStream(@"C:\temp\utf-8.txt", FileMode.Open, FileAccess.Read, FileShare.None); //初始化FileStream
static StreamReader sr3 = new StreamReader(myfs); //3.使用StreamReader(FileStream)
static StreamReader sr4 = new StreamReader(myfs, Encoding.UTF8);//4.使用StreamReader(FileStream, Encoding)指定编码方式,并指定编码方式 

static FileInfo myFile = new FileInfo(@"C:\temp\utf-8.txt"); //初始化FileInfo
StreamReader sr5 = myFile.OpenText();//5.使用File.OpenText(FilePath)
StreamReader sr6 = System.IO.File.OpenText(@"C:\temp\utf-8.txt");//6.使用FileInfo.OpenText()
//通过上面的代码初始化了一个StreamReader,然后可以每次读一行,也可以每次读一个字符 ,还可以每次读几个字符,甚至也可以一次将所有内容读完
#endregion


#region 以文本流的方式读取文本文件
public string ReadTxt(string txtFile)
{
    using (StreamReader reader = new StreamReader(txtFile))
    {
        int nextChar = reader.Read();// 读一个字符 
        int nCharsRead = reader.Read(new char[100], 0, 100);//读100个字符
        string nextLine = reader.ReadLine();// 读一行 
        return reader.ReadToEnd();//全部读完 
    }
}
#endregion

#region 以文本流的方式写入文本文件
public void WriteTxt(string txtFile, string content)
{
    using (FileStream fs = new FileStream(txtFile, FileMode.Create))
    {
        using (StreamWriter writer = new StreamWriter(fs))
        {
            writer.Write(content);
        }
    }
}
#endregion

标签:string,temp,FileStream,读写,文本文件,new,txt,StreamReader
From: https://www.cnblogs.com/jnmcok/p/17784589.html

相关文章

  • 解决Linux非root用户读写串口权限问题
    查看串口和基本设置查看串口:ls/dev/ttyUSB*查看参数:stty-F/dev/ttyUSB0设置波特率:stty-F/dev/ttyUSB0speed9600收发数据先打开后台接收:cat/dev/ttyUSB0&发送:echohello>/dev/ttyUSB0可以使用printf做更精确的控制:printf'hello\r'>/dev/ttyUSB0解决"P......
  • C++ 读写锁
    官网:https://zh.cppreference.com/w/cpp/thread/shared_mutex1.何为读写锁相比互斥锁,读写锁允许更高的并行性,互斥量要么锁住状态要么不加锁,而且一次只有一个线程可以加锁。读写锁可以有三种状态:读模式加锁状态;写模式加锁状态;不加锁状态;只有一个线程可以占有写模式的读写......
  • jdom读写xml
    要使用jdom解析xml文件,需要下载jdom的包,我使用的是jdom-1.1,附件中有。xml文件:<?xmlversion="1.0"encoding="UTF-8"?><sys-config> <jdbc-info> <driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name> <url>jd......
  • C语言 mmap完成文件读写
    #include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/mman.h>#include<fcntl.h>#include<unistd.h>intmain(){//打开文件进行读写intfd=open("test.log",O_RDWR|O_CREAT,0600);......
  • C#上位机序列9: 批量读写+事件广播
    1.读取配置文件及创建变量信息(点位名称,地址,数据类型(bool/short/int/float/long/double))2.读任务&写任务,数据有变化时事件广播通知usingHslCommunication;usingHslCommunication.Core;usingHslCommunication.ModBus;usingPLCEvent.Util;usingSystem;usingSystem.......
  • IO流,通过字节缓冲流来提高读写效率
    BufferedInputStream和BufferedOutputStream  两个流是缓冲字节流,通过内部缓存数组来提高操作流的效率。 当我们开启了很多流时,关闭顺序为:先开的后关闭(后开的先关闭)  在这个缓冲区中,byte数组的默认长度为8192,也是2的整数幂   练习代码如下: 结果是在指定文......
  • C#配置文件读写(App.config)
      /**************************************************描述:**Author:ys*Date:2023/10/1816:14:32*Update:*************************************************/usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingS......
  • Qt 读写文件操作
    一、Qt中的读文本的内容1.以QTextStream流的形式来读取文件中的内容。#include<QFile>#include<QTextStream>#include<QDebug>voidReadData(QStringfilePath){QFilefile(filePath);if(!file.exists()){qDebug()<<"can'......
  • 多线程编程同步:读写锁
    读写锁的定义互斥锁锁住后,保证仅有一个线程处理数据(多线程共享的)。要是数据的读取比写入更频繁,且读取操作不涉及共享变量的修改,应允许多个线程读取操作对共享变量的读取。直接使用互斥锁效率太低,若使用读写锁,可以大大提高效率。读写锁的分配规则:1)只要没有线程持有某个特定的读......
  • 【Linux常用命令5】文本文件编辑命令
    stat命令:查看文件详细信息的命令stat[选项]文件参数含义-L支持符号链接-f显示文件系统的信息-t以简洁的方式输出--help显示命令帮助信息--version显示命令版本信息示例:[root@izb1j05w8ldmtn1ylavvdfzmpaas]#statzhuque2tj.jsonFile:......