using System;
using System.Collections.Generic;
using System.IO;
using System.Text;namespace systemio
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FileStream fs = new FileStream("text.txt", FileMode.Open);
                long len = fs.Length;
                byte[] bytes = new byte[len];
                ASCIIEncoding asc = new ASCIIEncoding();
                int tmpint = fs.ReadByte();
                while (tmpint >= 0)
                {
                    int i = 0;
                    if (i < len)
                    {
                        bytes[i] = (byte)tmpint;
                        i++;
                    }
                    Console.Write(tmpint.ToString() + "\t");
                    tmpint = fs.ReadByte();                }                fs.Flush();
                fs.Close();
                char[] chars=asc.GetChars(bytes);
                Console.WriteLine();
                foreach (char cha in chars)
                {
                    
                    Console.Write(cha);
                }
                Console.ReadKey();
            }
            catch (FileNotFoundException fnf)
            {
                Console.WriteLine("异常信息为:" + fnf.Message);
            }
            catch (NotSupportedException nse)
            {
                Console.WriteLine("异常信息为:" + nse.Message);            }
            catch (ObjectDisposedException ode)
            {
                Console.WriteLine("异常信息为:" + ode.Message);
            }
            catch (ArgumentNullException ane)
            {
                Console.WriteLine("异常信息为:" + ane.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("异常信息为:" + e.Message);
 
            }
                    }
    }
}其中text.txt文件里的内容为“hello how are you?”字节asc码能顺利输出了,但是我试图用这程序把字节转化为字符输出,但是输出chars数组里内容为空的!

解决方案 »

  1.   

    编译能过了
    是否这项有问题??                       if (i < len) 
                        { 
                            bytes[i] = (byte)tmpint; 
                            i++; 
                        } 
      

  2.   

    while (tmpint >= 0) 
                    { 
                        int i = 0; 
                        if (i < len) 
                        { 
                            bytes[i] = (byte)tmpint; 
                            i++; 
                        } 
                        Console.Write(tmpint.ToString() + "\t"); 
                        tmpint = fs.ReadByte();                 } 
    每次循环回来i又被赋值0了.也就是你
    bytes[0]='h';
    bytes[0]='e'       //第二次循环就覆盖了
    bytes[0]='l'
    .
    .
    .
    bytes[0]='?'
    bytes[0]=空格
      

  3.   

    while (tmpint >= 0) 
                    { 
                        int i = 0; 
                        if (i < len) 
                        { 
                            bytes[i] = (byte)tmpint; 
                            i++; 
                        } 
                        Console.Write(tmpint.ToString() + "\t"); 
                        tmpint = fs.ReadByte();                 } 
    每次循环回来i又被赋值0了.也就是你
    bytes[0]='h';
    bytes[0]='e'       //第二次循环就覆盖了
    bytes[0]='l'
    .
    .
    .
    bytes[0]='?'
    bytes[0]=空格
      

  4.   

    谢谢sageking2我看头晕了,犯了低级错误