如何从文本文件里 指定行数开始读,从最后开始读几行。。。

zhangshuqiang123 2010-07-26 10:32:34
我想从文本文件中最后几行开始读,一直读到尾,这样怎么实现
...全文
1998 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangshuqiang123 2010-07-28
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 sdfkfkd 的回复:]

引用 3 楼 wuyazhe 的回复:

两种写法
C# code

//方法1
//按行读文件,跳过行
StreamReader reader = File.OpenText("文件");
int skip = 3;//假设跳过3行从第四行开始
string line = reader.ReadLine();
int current_line = 1;
while (li……
[/Quote]

这个方法没见过哇
liuweiqi 2010-07-27
  • 打赏
  • 举报
回复
其实现在很多类的成员函数的功能都是微软自己封装起来的 象你一些特殊的应用如果没有现成的只能自己写了 常用的方法微软都给你做好了~~~而已 不要那么懒 象我C语言开发底层的时候连某些数据结构都得去写~
lanfengsese 2010-07-27
  • 打赏
  • 举报
回复
学习了
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhangshuqiang123 的回复:]
方法2看起来好简单啊!!!
哈哈,有没有方法,定位到文件的末尾,然后,倒数几行开始读文件啊[/Quote]
例如:

string[] fs=File.ReadAllLines("文件")
string[] lines_array = fs.Skip(fs.count-4).ToArray();

这就是最后三行。
qinzhuhua 2010-07-27
  • 打赏
  • 举报
回复
路过,学习.....
特别 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyazhe 的回复:]

两种写法
C# code

//方法1
//按行读文件,跳过行
StreamReader reader = File.OpenText("文件");
int skip = 3;//假设跳过3行从第四行开始
string line = reader.ReadLine();
int current_line = 1;
while (line != null && current_……
[/Quote]
如果文件很大的话,第2种方法就不适合
可以以二进制方式打开,从文件末尾反序查找换行符
读取到BYTE数组,然后再转换为字符串
兔子-顾问 2010-07-26
  • 打赏
  • 举报
回复
没办法,那样只有你自己实现这个功能了。这不是常见操作。
wuyq11 2010-07-26
  • 打赏
  • 举报
回复
List<string> lines = new List<string>(File.ReadAllLines(""));
string[] arr=File.ReadAllLines("").Skip(1).ToArray();

zhangshuqiang123 2010-07-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyazhe 的回复:]
两种写法

C# code

//方法1
//按行读文件,跳过行
StreamReader reader = File.OpenText("文件");
int skip = 3;//假设跳过3行从第四行开始
string line = reader.ReadLine();
int current_line = 1;
while (line != null && ……
[/Quote]

方法2看起来好简单啊!!!
哈哈,有没有方法,定位到文件的末尾,然后,倒数几行开始读文件啊
兔子-顾问 2010-07-26
  • 打赏
  • 举报
回复
两种写法

//方法1
//按行读文件,跳过行
StreamReader reader = File.OpenText("文件");
int skip = 3;//假设跳过3行从第四行开始
string line = reader.ReadLine();
int current_line = 1;
while (line != null && current_line++ < skip)
{
line = reader.ReadLine();
}
//读取剩下所有行
List<string> lines = new List<string>();
while (line != null)
{
lines.Add(line);
line = reader.ReadLine();
}
//lines就是你要的

//方法2
//或是Linq写法
string[] lines_array = File.ReadAllLines("文件").Skip(3).ToArray();

Peter200694013 2010-07-26
  • 打赏
  • 举报
回复

using System;
using System.IO;
using System.Collections;

namespace brightheroes
{
/// <summary>
/// 本类主要是为了以行的形式来处理文本文件
/// </summary>
public class TxtHelper
{
private int _LineNumber;
private string _FilePath;

/// <summary>
/// 文件总行数
/// </summary>
public int LineNumber
{
get{return this._LineNumber;}
}

/// <summary>
/// 文件路径
/// </summary>
public string FilePath
{
get{return this._FilePath;}
}

private ArrayList fileLine;

/// <summary>
/// 读取文件到ArrayList里面去,按照行号排列
/// </summary>
/// <param name= "FilePath "> </param>
public TxtHelper(string FilePath)
{
this._FilePath = FilePath;
StreamReader sr = new StreamReader(FilePath);
fileLine = new ArrayList();
int i = 0;
while(sr.Peek() > -1)
{
fileLine.Insert(i,sr.ReadLine());
i = i + 1;
}

this._LineNumber = i;
sr.Close();
}


/// <summary>
/// 返回某一行的内容
/// </summary>
/// <param name= "LineIndex "> </param>
/// <returns> </returns>
public string ReadLine(int LineIndex)
{
return this.fileLine[LineIndex].ToString();
}

/// <summary>
/// 替换某一行的内容,如果不存在该行,则加入到末尾。
/// </summary>
/// <param name= "LineIndex "> </param>
/// <param name= "LineValue "> </param>
/// <returns> </returns>
public void WriteLine(int LineIndex,string LineValue)
{
if(LineIndex <= this._LineNumber)
{
this.fileLine[LineIndex] = LineValue;
}
else
{
this.fileLine.Insert(this._LineNumber,LineValue);
this._LineNumber += 1;
}
}

/// <summary>
/// 插入某行到某处
/// </summary>
/// <param name= "LineIndex "> </param>
/// <param name= "LineValue "> </param>
public void InsertLine(int LineIndex,string LineValue)
{
if(LineIndex <= this._LineNumber)
{
this.fileLine.Insert(LineIndex,LineValue);
}
else
{
this.fileLine.Insert(this._LineNumber,LineValue);
this._LineNumber += 1;
}
}

/// <summary>
/// 覆盖原文件
/// </summary>
public void Save()
{
StreamWriter sw = new StreamWriter(this._FilePath);
for(int i = 0;i <this.fileLine.Count;i++)
{
sw.WriteLine(this.fileLine[i]);
}
sw.Close();
}

/// <summary>
/// 生成新的文件
/// </summary>
/// <param name= "FilePath "> </param>
public void Save(string FilePath)
{
StreamWriter sw = new StreamWriter(FilePath);
for(int i = 0;i <this.fileLine.Count;i++)
{
sw.WriteLine(this.fileLine[i]);
}
sw.Close();
}
}
}
特别 2010-07-26
  • 打赏
  • 举报
回复
可能你得要从头开始读,只不过把前面的数据放弃而已

110,499

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧