王朝网络
分享
 
 
 

关于读取文本文件的一些文摘

王朝other·作者佚名  2006-01-29
宽屏版  字体: |||超大  

关于读取文本文件的一些文摘

http://community.csdn.net/Expert/topic/3274/3274491.xml?temp=.4443323

给 AntingZ(夕惕若)的补充一下,凡用StreamReader,请习惯带上System.Text.Encoding.Default,要不我们

的中文就不能正确显示了。

Dim file As New System.IO.StreamReader("c:\test.txt", System.Text.Encoding.Default)

Dim strLine As String

strLine = file.ReadLine() '读第1行

MessageBox.Show(strLine)

strLine = file.ReadLine() '读第2行

MessageBox.Show(strLine)

file.Close()

回车chr(13)

\r\n是换行回车

\n就回车

http://chs.gotdotnet.com/QuickStart/howto/default.aspx?url=/quickstart/howto/doc/anagrams.aspx

http://community.csdn.net/Expert/topic/2828/2828051.xml?temp=.508465

StreamWriter sw = File.AppendText(FilePath);

sw.Close()

StreamReader sr = new StreamReader(".\\说明.txt",Encoding.Default);

string input = "";

while((input = sr.ReadLine()) != null)

{

}

sr.Close()

http://community.csdn.net/Expert/topic/2814/2814490.xml?temp=.7782404

\修改

FileStream fs = new FileStream(filename,FileMode.OpenOrCreate,FileAccess.Write,FileShare.Read);

StreamWriter ww= new StreamWriter(fs,System.Text.Encoding.UTF8);

ww.BaseStream.Seek(-6,SeekOrigin.End);

ww.Write(text);.............

FileStream fs = new FileStream("c:\\test.txt",FileMode.OpenOrCreate,FileAccess.

ReadWrite,FileShare.Read);

StreamWriter ww = new StreamWriter(fs,System.Text.Encoding.

Default);

int pos = 6;

byte[] lastByte = new byte[pos];

ww.BaseStream.Seek(-pos,SeekOrigin.End);

fs.Read(lastByte,0,pos);

string lastStr = System.Text.Encoding.Default.GetString(lastByte

);

ww.BaseStream.Seek(-pos,SeekOrigin.End);

string appendStr = "BBBBBB";

ww.Write(appendStr+lastStr);

ww.Flush();

ww.Close();

fs.Close();

大文本文件计数

http://community.csdn.net/Expert/topic/2803/2803134.xml?temp=.6808435

int line_count=0;

try

{

StreamReader sw=new StreamReader(File_path,System.Text.Encoding.GetEncoding("GB2312"));

string line="";

while((line=sw.ReadLine())!=null)

{line_count+=1;}

sw.Close;

}

catch(Exception ex)

{MessageBox.Show(ex.Message);}

如何将一个文本文件的内容清空,但不删除这文件

http://community.csdn.net/Expert/topic/2788/2788580.xml?temp=.4243738

Dim writer As New StreamWriter("c:\sample.txt", False)

writer.Write(Nothing)

MessageBox.Show("一共有"+line_count.ToString()+"行");

C#写文本文件如何换行

http://community.csdn.net/Expert/topic/2725/2725488.xml?temp=.644848

webform ,c#

写文本文件。。

在后面加 \n , \n\r , \r\n 均不可以

出现状况为在win2K的记事本中换行的地方出现一个黑色的方块

但如果用 editplus等文本编辑软件打开则没有问题。。。。

应该怎么才能正确换行。。。。

vb.net中用 vbcrlf, c#中怎么用。。。

r\n should work

for example:

using System.IO;

StreamWriter sw = new StreamWriter("testfile.cs.txt",false, System.Text.Encoding.GetEncoding("GB

2312"));

sw.Write("hello\r\nworld");

sw.Close();

如何获得文本文件的行数?在线等

http://community.csdn.net/Expert/topic/2662/2662493.xml?temp=.7574121

StreamReader sr = new StreamReader(filename);

int line = 0;

while (sr.ReadLine() != null) {

line++;

}

已经很完整了,另一个方法:这个方法可以扩展很多其他的应用:

对于学习熟悉文件结构很有好处:

System.IO.StreamReader xx=new System.IO.StreamReader("F:\\a.txt");

int getB=xx.BaseStream.ReadByte();

int add=1;

while(getB!=-1)

{

if(getB==13 && xx.BaseStream.ReadByte()==10){add++;xx.BaseStream.Position--;}

getB=xx.BaseStream.ReadByte();

}

this.label1.Text=add.ToString();

To bankliu(水岸)

其实单单只有字符'\n'或者'\r'也算换行,所以应该是这样:

while(getB!=-1)

{

if(getB==13 || getB ==10){

add++;

if (getB == 13 && xx.BaseStream.ReadByte()==10)

xx.BaseStream.Position++;

}

getB=xx.BaseStream.ReadByte();

}

20040827

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有