首页 > 编程语言 >C#读取txt文本文件的几种方式

C#读取txt文本文件的几种方式

时间:2022-12-19 17:38:16浏览次数:35  
标签:string C# sr 文本文件 words new txt public 读取


1.按行读取:每次读取一行内容,即遇到回车键才会停止。

public void readfile(string filepath)
{
if (!File.Exists(filepath))
{
MessageBox.Show(" file not exits !");
return;
}
string[] result ;
FileStream fs = new FileStream(filepath, FileMode.Open);
using (StreamReader sr = new StreamReader(fs, Encoding.Default))
{
List<string> list = new List<string>();
string input;
while (!sr.EndOfStream)
{
input= sr.ReadLine().ToString();

}
}

sr.Close();
}

2. 理论上任意形式的读取方式

using System;

public class SplitTest {
public static void Main() {

string words = "This is a list of words, with: a bit of punctuation" +
"\tand a tab character.";

string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

foreach (string s in split) {

if (s.Trim() != "")
Console.WriteLine(s);
}
}
}
// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character



标签:string,C#,sr,文本文件,words,new,txt,public,读取
From: https://blog.51cto.com/u_15917617/5953244

相关文章

  • C++ Assert()断言机制原理以及使用
    机器学习以及人工智能的学习需要扎实的数学功底才能走的更远,爬的更高,所以打好数学基础是关键,但无论工作学习都没有充足的时间去拿着书本一个字一个字的去学习了,这里我建议大......
  • C#中readonly与const区别
        1.const、readonly和staticreadonly定义的常量,指定初始值后(包括在构造函数内指定的初始值) 将不可更改,可读不可写;        2.const定义时必须指定初......
  • C# dataGridView常用功能
     1.鼠标选中一行删除此行: foreach(DataGridViewRowrindGView_countList.SelectedRows)       {        if(!r.IsNewRow)   ......
  • linux C的一些笔记
    1.主函数的入参 主函数类似定义intmain();intmain(intargs);intmain(intargs,char*av[]);可以无惨,可以接受参数输入,入参一般为命令行执行参数,比如  传......
  • C#-sealed 封装类的使用
    示例1:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacesingleton{publicclassmyClass1{///<summary>//......
  • MATLAB随机波动率SV、GARCH用MCMC马尔可夫链蒙特卡罗方法分析汇率时间序列|附代码数据
    全文下载链接:http://tecdat.cn/?p=27340 最近我们被客户要求撰写关于分析汇率的研究报告,包括一些图形和统计输出。波动率是一个重要的概念,在金融和交易中有许多应用。它......
  • C#之lock关键字 lock锁定语句
    lock下面的示例包含一个lockclassAccount{decimalbalance;privateObjectthisLock=newObject();publicvoidWithdraw(decimalamount)......
  • VS2010创建基于C/S的三层架构模型
    在VS2010环境下创建基于C/S架构的三层模型:基础知识准备:    三层:UI、BLL、DAL另加数据模型Model    Winform作为UI界面。1、创建空的解决方案      新......
  • opencv对于bmp图像像素值的读取和赋值
    #include<stdio.h>#include<cv.h>#include<highgui.h> voidmain(){   IplImage*img=cvLoadImage("c://fruitfs.bmp",1);   CvScalars;///获取像素值  ......
  • CV520国产替代Ci521 13.56MHz 非接触式读写器芯片-更低成本替代
    CI521是一个高度集成的,工作在13.56MHz的非接触式读写器芯片,阅读器支持ISO/IEC14443A/B/MIFARE。CI521无需外围其他电路,Ci521的内部发送器可驱动读写器天线与ISO/I......