实现的功能是读取文本文件的最后一行:
try 
    { 
    File file=new File("filename.txt"); 
    FileReader fr=new FileReader(file); 
    BufferedReader br=new BufferedReader(fr); 
    int len=(int)file.length(); 
    boolean flag=true; 
    int num_temp=0; 
    //String str=null; 
    char chars[]=new char[1000];/*不一定是len,可以定义一个比较大的数也可以。只要比估计的一行字符的个数要大就行!!!*/ 
    char ch; 
    while(flag) 
    { 
      num_temp=br.read(chars,len,len--); 
      if(chars[0].equals("\n")) 
        flag=false; 
    } 
    num_temp=br.read(chars,len+1,file.length()); 
    } 
    catch(IOException e) 
    { 
      System.out.println("IO Error!"); 
    } 
    System.out.println("你的文件的最后一行的字符是:\n"+chars.toString()); 

解决方案 »

  1.   

    string strLastLine = string.Empty;
    try 

    System.IO.FileStream file=System.IO.File.OpenRead(@"c:\ss.txt");  System.IO.StreamReader sr = new System.IO.StreamReader(file);

    bool flag = true;
    do
    {
    string str = sr.ReadLine();
    if (str!=null)
    strLastLine = str;
    else
    flag = false; }while(flag); } 
    catch(System.IO.IOException ex) 

    Console.WriteLine("IO Error!"); 

    Console.WriteLine("你的文件的最后一行的字符是:\n"+strLastLine);
      

  2.   

    string bFilePath ="filename.txt";
    FileStream fsMyfile = new FileStream(bFilePath, FileMode.Open, FileAccess.Read);
    StreamReader srMyfile= new StreamReader(fsMyfile);
    srMyfile.BaseStream.Seek(0, SeekOrigin.Begin);
    string key="";
    int j=0;
    while((key = srMyfile.ReadLine())!=null)
    {

    for(int i=0; i<key.Length; i++)
    {
    dataValue[j++] = key.ToString();
    }
    }
    srMyfile.Close();
    fsMyfile.Close();