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

类别:.NET开发 点击:0 评论:0 推荐:

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


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

 

 

本文地址:http://com.8s8s.com/it/it44380.htm