오늘의 C# 공부 : 파일 다루기, 스트림.

//////////////////////////////////////////////////
/////파일 다루기 /////////////////////////////////
/////////////////////////////////////////////////

//// 디렉토리 / 파일 정보 조회하기

//using System;
//using System.Linq;
//using System.IO;

//namespace Dir
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            string directory;
//            if (args.Length < 1)
//                directory = “.”;
//            else
//                directory = args[0];

//            Console.WriteLine("{0} directory Info", directory);
//            Console.WriteLine("-Directories : “);

//            var directories = (from dir in Directory.GetDirectories(directory)
//                               let info = new DirectoryInfo(dir) //let이 뭐더라
//                               select new
//                               {
//                                   Name = info.Name,
//                                   Attributes = info.Attributes
//                               }).ToList();//이 부분에 대한 설명 필요

//            foreach (var d in directories)
//                Console.WriteLine ( “{0}:{1}”, d.Name, d.Attributes);

//            Console.WriteLine("-Files :”);
//            var files = (from file in Directory.GetFiles(directory)
//                         let info = new FileInfo(file)
//                         select new
//                         {
//                             Name = info.Name,
//                             FileSize = info.Length,
//                             Attributes = info.Attributes
//                         }).ToList();

//            foreach ( var f in files)
//                Console.WriteLine ( “{0} : {1},{2}",f.Name, f.FileSize, f.Attributes);
                        
//        }
//    }
//}

////디렉토리 / 파일 생성하기
//using System;
//using System.IO;

//namespace Touch
//{
//    class MainApp
//    {
//        static void OnWrongPathType(string path)
//        {
//            Console.WriteLine(“Type{0} is Wrong Type”);
//            //Console.WriteLine(“Type{0} is Wrong Type”,path);
//            return;
//        }

//        static void Main(string[] args)
//        {

//            if (args.Length == 0)
//            {
//                Console.WriteLine(
//                    “Usage : Touch.exe [Type : File / Directory]”);
//                return;
//            }

//            string path = args[0];

//            string type = “File”;
//            if (args.Length > 1)
//                type = args[1];

//            if (File.Exists(path) || Directory.Exists(path))
//            {
//                if (type == “File”)
//                    File.SetLastWriteTime(path, DateTime.Now);
//                else if (type == “Directory”)
//                    Directory.SetLastWriteTime(path, DateTime.Now);
//                else
//                {
//                    OnWrongPathType(path);
//                    return;
//                }

//                Console.WriteLine(“Updated {0} {1}”, path, type);

//                return;
//            }

//            if (type == “File”)
//                File.Create(path).Close();
//            else if (type == “Directory”)
//                Directory.CreateDirectory(path);
//            else
//            {
//                OnWrongPathType(path);
//                return;
//            }

//            Console.WriteLine(“Create {0} {1}”, path, type);
//        }
//    }
//}

////파일스트림으로 데이터 쓰고 읽기
//using System;
//using System.IO;

//namespace BasicIO
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            long someValue = 0x123456789ABCDEF0;
//            Console.WriteLine ("{0,-13} : 0x{1:X16}”,“Original Data”, someValue);

//            Stream outStream = new FileStream(“a.dat”, FileMode.Create);
//            byte[] wBytes = BitConverter.GetBytes(someValue);

//            Console.Write("{0,-13} : “, “Byte array”);

//            foreach (byte b in wBytes)
//                Console.Write("{0:X2} “, b);
//            Console.WriteLine();

//            outStream.Write(wBytes, 0, wBytes.Length);
//            outStream.Close();

//            Stream inStream = new FileStream(“a.dat”,FileMode.Open);
//            byte[] rbytes = new Byte[8];

//            int i = 0;
//            while (inStream.Position < inStream.Length)
//                rbytes[i++] = (byte)inStream.ReadByte();

//            long readValue = BitConverter.ToInt64(rbytes, 0);

//            Console.WriteLine("{0,-13}  : 0X{1:X16}  “, “Read Data”, readValue);

//            inStream.Close();

//        }

//    }
//}

////파일스트림에서 임의접근하는 예제 프로그램
//using System;
//using System.IO;

//namespace SeqNRand
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Stream outStream = new FileStream(“a.dat”, FileMode.Create);

//            Console.WriteLine(“Position  : {0} “, outStream.Position);

//            outStream.WriteByte(0x01);

//            Console.WriteLine(“Position  : {0} “, outStream.Position);

//            outStream.WriteByte(0x02);

//            Console.WriteLine(“Position  : {0} “, outStream.Position);

//            outStream.WriteByte(0x03);

//            Console.WriteLine(“Position  : {0} “, outStream.Position);

//            outStream.Seek(5, SeekOrigin.Current);
//            Console.WriteLine(“Position  : {0} “, outStream.Position);

//            outStream.WriteByte(0x04);
//            Console.WriteLine(“Position  : {0} “, outStream.Position);

//            outStream.Close();
//        }
//    }
//}

////바이너리 리더

//using System;
//using System.IO;

//namespace BinaryFile
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            BinaryWriter bw = new BinaryWriter(new FileStream(“a.dat”, FileMode.Create));

//            bw.Write(int.MaxValue);
//            bw.Write(“Good Morning”);
//            bw.Write(uint.MaxValue);
//            bw.Write(“안녕하세요”);
//            bw.Write(double.MaxValue);

//            bw.Close();

//            BinaryReader br = new BinaryReader(new FileStream(“a.dat”,FileMode.Open));

//            Console.WriteLine(“File size : {0}  bytes” , br.BaseStream.Length);
//            Console.WriteLine("{0}”, br.ReadInt32());
//            Console.WriteLine("{0}”, br.ReadString());
//            Console.WriteLine("{0}”, br.ReadUInt32());
//            Console.WriteLine("{0}”, br.ReadString());
//            Console.WriteLine("{0}”, br.ReadDouble());

//            br.Close();
//        }
//    }
//}

////텍스트 처리를 위한 스트림 리더
//using System;
//using System.IO;

//namespace TextFile
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            StreamWriter sw = new StreamWriter(new FileStream(“a.txt”, FileMode.Create));

//            sw.WriteLine(int.MaxValue);
//            sw.WriteLine(“Good Morning”);
//            sw.WriteLine(uint.MaxValue);
//            sw.WriteLine(“안녕하세요”);
//            sw.WriteLine(double.MaxValue);

//            sw.Close();

//            StreamReader sr = new StreamReader(new FileStream(“a.txt”, FileMode.Open));

//            Console.WriteLine(“FileSize : {0}”, sr.BaseStream.Length);

//            while (sr.EndOfStream == false)
//            {
//                Console.WriteLine(sr.ReadLine());
//            }

//            sr.Close();
//        }
//    }
//}

Hugo로 만듦
JimmyStack 테마 사용 중