逐个读取文件中所有行的 C# 程序
使用 ReadAllLines() 方法逐个读取文件中所有行。
假设有一个文件“new.txt”,其中包含以下行。
One Two Three
首先,设置要读取的文件的路径。
String myPath = "new.txt";
现在将其添加到一个字符串数组中,逐个获取行。
String[] fLine = File.ReadAllLines(myPath);
假设你需要获取第一行。为此。
fLine[0]
以下是逐个读取文件中行的完整示例。
示例
using System;
using System.IO;
public class Demo {
public static void Main() {
String myPath = "new.txt";
String[] fLine;
// array of lines in a file
fLine = File.ReadAllLines(myPath);
// read lines of a file
Console.WriteLine("Line 1: "+fLine[0]);
Console.WriteLine("Line 2: "+fLine[1]);
Console.WriteLine("Line 3: "+fLine[2]);
Console.WriteLine("Line 4 "+fLine[3]);
}
}输出
Line1: One Line2: Two Line3: Three Line4: Four
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP