C#读取txt文本文件的几种方式
2014-04-26 00:50
645 查看
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. 理论上任意形式的读取方式
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
相关文章推荐
- selenium读取txt文件的几种方式
- C#简单实现读取txt文本文件并分页存储到数组
- C#读取固定文本格式的txt文件
- MATLAB处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- C#简单实现读取txt文本文件并分页存储到数组
- matlab处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- C# 将 txt 文件拖拽到 textbox时 textbox 读取txt中文本内容
- C#读取txt文本文件(dat)的方法
- c#写对象来读取TXT文本文件
- C# Python 读取txt中的文件,将16进制的数据按照小端方式排列,处理为有符号的数字,并写入text文件;控制台和界面两种方式
- C#读取txt文本文件(dat)的方法
- c#实现对MP4等文件属性信息的读取以及对txt文件内容的读取
- C#中如何将信息保存到txt文件中去,以及如何读取
- Java读取文件内容的几种方式
- C#窗体中,textbox中数据保存到txt文本中,从文本读取数据到textbox
- 读取五种格式的配置文件(xml(两种方式),txt,excel,csv,json)
- c# 读取文件内容存放到int数组 array.txt
- Java 读取TXT文件的多种方式
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例