zoukankan      html  css  js  c++  java
  • 操纵txt文本文件

    txt文本文件(ticket.txt)的格式:
    666666,Alfred Lv,Resigned,06/24/2010 02:13:30 PM ZE8
    99999,Alfred Lv,Closed,06/24/2010 02:13:45 PM ZE8

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.Text;
    
    namespace WebApplication1
    {
        class FileUtilitity
        {
            public Ticket[] ReadFile()
            {
                string filename = "D:\\Ticket.txt";
                string[] Tickets = new string[4];
                List<Ticket> ticket = new List<Ticket>();
             
    
             
                //打开文件并显示其内容 
                StreamReader reader = null;
                try
                {
                    reader = new StreamReader(filename);
                    for (string line = reader.ReadLine(); !String.IsNullOrEmpty(line); line = reader.ReadLine())
                    {
                        Tickets = line.Split(',');
                        Ticket tmpTicket= new Ticket();
                        tmpTicket.TicketNo = Tickets[0];
                        tmpTicket.Author = Tickets[1];
                        tmpTicket.Status = Tickets[2];                    
                        tmpTicket.LastModified = Tickets[3];
                        ticket.Add(tmpTicket);                   
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
                return ticket.ToArray();
            }
    
            public void WriteFile(string s, string path)
            {
                FileStream fout = new FileStream(path, FileMode.Append, FileAccess.Write);
                StreamWriter brout = new StreamWriter(fout, Encoding.Default);
                brout.WriteLine(s);
                brout.Close();
            }
        }
    
        public class Ticket
        {
            public string TicketNo
            {
                get;
                set;
            }
    
            public string Status
            {
                get;
                set;
            }
    
            public string Author
            {
                get;
                set;
            }
            public string LastModified
            {
                get;
                set;
            }
        }
    }
    
    
  • 相关阅读:
    大话设计模式-原型模式
    Unity3D实习生面试题总结-图形学相关
    UnityShader入门精要-渲染流水线
    大话设计模式-工厂方法模式
    大话设计模式-代理模式
    C#中的值类型和引用类型
    【树结构】树链剖分简单分析
    HEOI2016排序-解题报告
    普通筛法时间界的证明
    可持久化线段树
  • 原文地址:https://www.cnblogs.com/mingle/p/CSharp_txt.html
Copyright © 2011-2022 走看看