그러냐

ArrayList 파일 IO 본문

c#

ArrayList 파일 IO

관절분리 2016. 1. 27. 18:34
반응형

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization;
namespace ConsoleApplication2
{
   
    class Program
    {
        static void Main(string[] args)
        {
            Class1 h = new Class1("1", "2", "3");
            BinaryFormatter format = new BinaryFormatter();
            try
            {
                Stream s = File.Open("c:\\2.txt", FileMode.Create, FileAccess.ReadWrite);
                format.Serialize(s, h);
                s.Close();

                s = File.Open("c:\\2.txt", FileMode.Open, FileAccess.Read);
             //   formatSerialize(s);
                object o = format.Deserialize(s);
                Class1 hh = (Class1)o;
                hh.Print();

            }
            catch (IOException e)
            {
                Console.WriteLine("파일 만드는 중 에러");
                Console.WriteLine(e.Message);
            }

            catch (SerializationException e)
            {
                Console.WriteLine("바이너리변환 중 에러");
                Console.WriteLine(e.Message);
            }

            catch (Exception e)
            {
                Console.WriteLine("알 수 없는 에러");
                Console.WriteLine(e.Message);
            }
            /*     ArrayList A = new ArrayList();
                 A.Add("");
                 A.Add("");
                 A.Add("");
     //            Print();


                 BinaryFormatter format = new BinaryFormatter();
                 try
                 {
                     //파일저장
                     Stream s = File.Open("c:\\1.txt", FileMode.Create, FileAccess.ReadWrite);
                     format.Serialize(s, A);
                     s.Close();
                     //파일출력
                     s = File.Open("c:\\1.txt", FileMode.Open, FileAccess.Read);
                     Object o = format.Deserialize(s);
                     s.Close();
                 }
                 catch(IOException e)
                 {
                     Console.WriteLine("에러");
                     Console.WriteLine(e.Message);
                 }
                 catch(SerializationException e)
                 {
                     Console.WriteLine("에러");
                     Console.WriteLine(e.Message);
                 }
               

             }
             public static void Print(String info, ArrayList h)
             {
                 Console.WriteLine(info);
                 //foreach(object a in h)
             }*/
        }
    }
}

 

----------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    [Serializable]
    class Class1
    {
        String name, phone, addr;
        public Class1(String name, String phone , String addr)
        {
            this.name = name;
            this.phone = phone;
            this.addr = addr;
       
        }
        public void Print()
        {
            Console.WriteLine("이름 ; " + name);
            Console.WriteLine("전번 ; " + phone);
            Console.WriteLine("주소 ; " + addr);
        }

    }
}

 

반응형

'c#' 카테고리의 다른 글

동적 배열  (0) 2016.01.27
[WCF]데이터 보내기1  (0) 2016.01.27
합계구한 컬럼을 DataTable에 추가하기  (0) 2016.01.27
C#에서의 IO [3/3]  (0) 2016.01.27
C#에서의 IO [2/3]  (0) 2016.01.27