逐行读取txt文件,使用Linq与StreamReader的Readline方法

List<string[]> list = File.ReadLines("YourFile.txt")
.Select(r => r.TrimEnd(‘#‘))
.Select(line => line.Split(‘,‘))
.ToList();

or

List<string[]> list = File.ReadLines("YourFile.txt")
.Select(r => r.TrimEnd(‘#‘).Split(‘,‘))
.ToList();

File.ReadLines would read the file line by line.

.Select(r => r.TrimEnd(‘#‘)) would remove the # from end of the line
.Select(line => line.Split(‘,‘)) would split the line on comma and return an array of string items.
ToList() would give you a List<string[]> back.

using System;

public class Example
{
   public static void Main()
   {
      string[] separators = {",", ".", "!", "?", ";", ":", " "};
      string value = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate.";
      string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries);
      foreach (var word in words)
         Console.WriteLine(word);
   }
}
StreamReader sr = new StreamReader(@monsterLocation);
            int searchId = monsterId;
            int actualId = 0;
            string name = "(Not found)";
            string[] details = null;
            string line = null;
            while ((line = sr.ReadLine()) != null)
            {
                line = line.Trim();
                if (line == "") continue;
                details = line.Split(‘\t‘);
                actualId = int.Parse(details[0]);
                if (actualId == searchId)
                {
                    name = details[2].Replace("\"", "");
                    break;
                }
            }
            sr.Close();
            Messagebox.shou("Result:" +name);
 
时间: 2024-10-20 12:30:39

逐行读取txt文件,使用Linq与StreamReader的Readline方法的相关文章

JAVA逐行读取TXT文件

package help; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.Ha

逐行读取txt文件

header("Content-type:text/html; charset=utf-8"); $handle = fopen('test.php', 'r') or die('Unable to file!'); $keyarr = array(); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); $buffer = str_replace(',', ',', $buffer); $bu

c#逐行读取txt文件

ArrayList lst = new ArrayList(); StreamReader sr = new StreamReader(openFileDialog1.FileName); while (!sr.EndOfStream) { string str = sr.ReadLine(); lst.Add(str); }

Python读取txt文件

Python读取txt文件,有两种方式: (1)逐行读取 1 data=open("data.txt") 2 line=data.readline() 3 while line: 4 print line 5 line=data.readline() (2)一次全部读入内存 1 data=open("data.txt") 2 for line in data.readlines(): 3 print line

JAVA读取TXT文件、新建TXT文件、写入TXT文件

1.创建TXT文件 按照正常的逻辑写就好 先定义一个文件给定一个路径——>判断这个路径上这个文件存不存在——>若不存在则建立,try/catch根据程序提示自动生成就好 2.读取TXT文件 思路1: 获得一个文件路径 以这个路径新建一个文件,判断这个文件是否以正常的文件格式存在着 以这个路径创建一个阅读缓冲器:FileInputStream——>InputStreamReader——>BufferedReader 逐行判断内容是否为空,将读取结果累加到一个字符串(result)上

C#读取txt文件

       用C#读取txt文件时,当txt中数据的分隔符是空格,而且空格数量不等时,如果直接用Split直接对每行进行分隔,那么list的数目会比每行的列数多,并不是真实的每行的列数.这时要把不等数目的空格全变成1个空格,这样计数才正确.代码如下: public static List<string>split(string s,char splitchar=' ') { return s.Split(splitchar).Select(p=>p.Trim()).Where(p=&g

.Net常用技巧_C#写入和读取txt文件

using System.Text.RegularExpressions; //脱机数据导出 private void writeTxt(string str) { string strFileName = @"\Program Files\firexungengcard\FireXungeng.txt"; FileInfo file = new FileInfo(strFileName); StreamWriter sWriter; if (file.Exists) { sWrite

关于读取txt文件中文乱码问题

在处理文件的过程中,读取txt文件出现中文乱码.这种情况是由于编码字符不一致导致. public static string ReadFile(string path, string fileName) { FileStream stream = null; StreamReader reader = null; StringBuilder v = new StringBuilder(); try { stream = new FileStream(path + fileName, FileMo

用C#读取txt文件的方法

1.使用FileStream读写文件 文件头: using System;using System.Collections.Generic;using System.Text;using System.IO; 读文件核心代码: byte[] byData = new byte[100];char[] charData = new char[1000]; try{FileStream sFile = new FileStream("文件路径",FileMode.Open);sFile.S