IO(C#) 读写文件简单操作

首先我们有这格式的一个记事本

学号    数学    语文     英语
001    80  90  100
002    90  90  100
003    70  80   
004    80  90  90
005    95  95  90

下来我们对此进行简单处理

下面例子是采用 主要是采用将文件读入之后将文件分割成一个一个字符串形式,存放在集合中。所以可以简单的控制其输出格式

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

//集合

using System.Collections;
namespace display
{
    class handle
    {
        private static List<students> list = new List<students>();
        static void Main(string[] args)
        {
            handle p = new handle();
            list = p.readr();

            //序号 学号 平均分 数学 语文 英语
            //1  001  90  90  90  90
            //2  002  80  70  90  0
            Console.WriteLine("直接读出来");
            Console.WriteLine("b、文件格式为:");
            p.writefile();
            p.readerfile();
            Console.WriteLine("经过处理之后");
            p.getre();
            Console.WriteLine();
            Console.WriteLine(@"I、 按照平均分降序格式化输出:");
            Console.WriteLine("序号    学号       平均分           数学  语文     英语");
            for (int i = 0; i < list.Count; i++)
            {
                int sum = 0;
                int avg = 0;
                Console.Write(i.ToString()+"        ");
                Console.Write(list[i].Str_id+"          ");
                //求平均分
                sum += Convert.ToInt32(list[i].Str_SX) + Convert.ToInt32(list[i].Str_YW) + Convert.ToInt32(list[i].Str_YY);
                avg = sum / 3;
                Console.Write(avg+"          ");
                Console.Write(list[i].Str_SX+"         ");
                Console.Write(list[i].Str_YW+"         ");
                Console.WriteLine(list[i].Str_YY+"        ");
            }
           
            p.gettwo();
            Console.WriteLine();
            Console.WriteLine();
            p.getthree();
           
             
            Console.ReadKey();
           
        }
        public void getre()
        {
            Console.WriteLine("学号 数学  语文  英语");
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Str_id != null)
                {
                    Console.Write(list[i].Str_id + "\t");
                }
                if (list[i].Str_SX != null)
                {
                    if (list[i].Str_id == "004")
                    {
                        Console.Write("\t");
                    }
                    else
                    {
                        Console.Write(list[i].Str_SX + "\t");
                    }
                }
                if (list[i].Str_YW != null)
                {
                    if (list[i].Str_id == "001")
                    {
                        Console.Write("\t");
                    }
                    else
                    {
                        Console.Write(list[i].Str_YW + "\t");
                    }
                }
                if (list[i].Str_YY != null)
                {
                    Console.WriteLine(list[i].Str_YY + "\t");
                }
                else
                {
                    Console.WriteLine("\t");
                }
            }
        }
             
        public void getthree() 
        {
            int avgnum = 0;
            Console.WriteLine(@"III、计算英语考试平均成绩,格式化输出");
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Str_YY!=null)
                {
                    avgnum += Convert.ToInt32(list[i].Str_YY.ToString());
                }
                else
                {
                    avgnum += 0;
                }
            }
            Console.WriteLine("英语平均成绩:"+(avgnum/Convert.ToInt32(list.Count)));
        }
        public void gettwo() 
        {
            Console.WriteLine("II、按照优秀率(>=85分为优秀)降序格式化输出,如:");
            //II、按照优秀率(>=85分为优秀)降序格式化输出,如:
            //序号 学号 优秀率 数学 语文 英语
            //1  001  100% 90  90  90
            //2  002  33%  90  70  70
            Console.WriteLine("序号       学号     优秀率      数学      语文   英语");
            int num = 0;
            string file = "";
            for (int i = 0; i < list.Count; i++)
            {
                Console.Write(i.ToString()+"          ");
                Console.Write(list[i].Str_id.ToString()+"         ");
                if (Convert.ToInt32(list[i].Str_SX) >= 85)
                {
                    num = num + 1;
                }
                if (Convert.ToInt32(list[i].Str_YW) >= 85)
                {
                    num = num + 1;
                }
                if (Convert.ToInt32(list[i].Str_YY) >= 85)
                {
                    num = num + 1;
                }
                //file = ((double)100 /num).ToString() + "%";
                if (num == 1) 
                {
                    file = "33%";
                } 
                if (num==2)
                {
                    file = "66%";
 
                }
                if (num==3)
                {
                    file = "100%";
                }

                Console.Write(file+"            ");
                
                if (list[i].Str_SX!=null)
                {
                    Console.Write(list[i].Str_SX.ToString() + "       ");    
                }
                else
                {
                    Console.WriteLine("\t");
                       
                }
                if (list[i].Str_YW!=null)
                {
                    Console.Write(list[i].Str_YW.ToString() + "       ");
                }
                else
                {
                    Console.WriteLine("\t");
                }
                if (list[i].Str_YY!=null)
                {
                    Console.WriteLine(list[i].Str_YY.ToString() + "      ");
                }
                else
                {
                    Console.WriteLine("\t");
                }
                num = 0;
            }
        }
        private void writefile()
        {
            try
            {
                string da = DateTime.Now.Year.ToString();
                // 文件流
                FileStream file = new FileStream("Students.txt", FileMode.Create, FileAccess.Write);
                //写入流
                StreamWriter strwriter = new StreamWriter(file);
                strwriter.BaseStream.Seek(0, SeekOrigin.Begin);
                strwriter.WriteLine("学号    数学    语文     英语");
                strwriter.WriteLine("001    80  90  100");
                strwriter.WriteLine("002    90  90  100");
                strwriter.WriteLine("003    70  80   ");
                strwriter.WriteLine("004    80  90  90");
                strwriter.WriteLine("005    95  95  90");
                strwriter.Close();
                file.Close();
            }
            catch (Exception)
            {
                throw;
            }
            //Console.ReadKey();
        }

        private void readerfile() 
        {
            try
            {
                List<object> obj = new List<object>();
                FileStream finle = new FileStream("Students.txt", FileMode.Open);
                byte[] by = new byte[500];
                char[] ch = new char[500];
                finle.Seek(0, SeekOrigin.Begin);
                finle.Read(by, 0, 500);
                Decoder d = Encoding.UTF8.GetDecoder();
                d.GetChars(by, 0, by.Length, ch, 0);
                Console.WriteLine(ch);
                finle.Close();
                string[] str = new string[500];
                
                List<string> List = new List<string>();
                for (int i = 0; i < ch.Length; i++)
                {
                    //if (ch[i].ToString()!=null)
                    //{
                    //    List.Add(ch[i].ToString());
                    //    for (int j = 0; j < ch.Length; j++)
                    //    {
                    //        str[j] = List[i].ToString();
                    //    }
                    //}
                    if (ch[i].ToString() == "\t" || ch[i].ToString() == " " || ch[i].ToString() == "\n" || ch[i].ToString() == "\r" || ch[i].ToString() == "\r")
                    {
                       
                    }
                    else
                    {
                        str[i] = ch[i].ToString();
                    }                    
                }
                //students _strtduents ;
                //string[] _str = null;
                构建表结构
                //for (int i = 0; i < str.Length; i++)
                //{
                //    _str = new string[5];
                //   _strtduents = new students();
                //   _strtduents.Str_id = "";
                //   _strtduents.Str_SX = "";
                //   _strtduents.Str_YW = "";
                //   _strtduents.Str_YY = "";
                //}
              //  Console.ReadKey();
                //while ((str = reader.ReadLine()) != null)
                //{
                //    Console.WriteLine(str);
                //    //_strtduents=new students();
                //    //_strtduents.Str_id = "";
                //    //_strtduents.Str_SX = "";
                //    //_strtduents.Str_YW = "";
                //    //_strtduents.Str_YY = "";
                //}
            }
            catch (Exception)
            {
                throw;
            }
          //  Console.ReadKey();
            
        }

        public List<students> readr()
        {
            List<students> lisy = new List<students>();
            students st = null;
            try
            {
                StreamReader reader = new StreamReader(@"E:\myCsharp\txt.txt", Encoding.UTF8);
                string strreader = string.Empty;
                strreader = reader.ReadLine();
                string s = "";
                //int a = 0;
                while ((strreader = reader.ReadLine()) != null)
                {
                    string[] words = strreader.Split('\t');
                    string[] w=new string[4];
                    int a = 0;
                    for (int i = 0; i < words.Length; i++)
                    {
                        if (words[i].ToString().Trim().Length == 0 || words[i].ToString().Trim() ==null)
                        {
                            a--;
                        }
                        else
                        {
                            if (words[i].ToString()!=null)
                            {
                                w[a] = words[i].ToString();
                            }
                            else
                            {
                                w[a] = "";
                            }
                            
                        }
                        a++;
                    }
                    st = new students();
                    if (w[0].ToString()!=null)
                    {
                        st.Str_id = w[0].ToString();
                    }
                    if (w[1].ToString()!=null)
                    {
                        st.Str_YW = w[1].ToString();
                    }
                    if (w[2].ToString()!=null)
                    {
                        st.Str_SX = w[2].ToString();
                    }
                    if (w[3]!=null )
                    {
                        st.Str_YY = w[3].ToString();
                    }
                    lisy.Add(st);
                }
                byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return lisy;
        }
    }
    public class students 
    {
        private string str_id;

        public string Str_id
        {
            get { return str_id; }
            set { str_id = value; }
        }
        private string str_SX;

        public string Str_SX
        {
            get { return str_SX; }
            set { str_SX = value; }
        }
        private string str_YW;

        public string Str_YW
        {
            get { return str_YW; }
            set { str_YW = value; }
        }
        private string str_YY;

        public string Str_YY
        {
            get { return str_YY; }
            set { str_YY = value; }
        }
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值