'C#'에 해당되는 글 88건
- 2018.06.07
- 2018.06.07
- 2018.06.07
- 2018.05.31
- 2018.05.31
- 2018.05.31
- 2018.05.31
- 2018.05.24
- 2018.05.24
- 2018.05.24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | using System; using System.IO; using System.Text; namespace FileStreamTest { class FileStreamTest { public static byte[] WriteStringBytes(string str, FileStream fs) { byte[] info = new UTF8Encoding(true).GetBytes(str); fs.Write(info, 0, info.Length); return info; } static void Main(string[] args) { FileStream fs = new FileStream("test.txt", FileMode.Create); fs.Seek(0, SeekOrigin.Begin); FileStreamTest.WriteStringBytes("------------------\r\n", fs); FileStreamTest.WriteStringBytes("---File Content---\r\n", fs); FileStreamTest.WriteStringBytes("------------------\r\n", fs); fs.Flush(); fs.Close(); byte[] b = new byte[1024]; FileStream fs2 = File.OpenRead("test.txt"); UTF8Encoding utf8 = new UTF8Encoding(true); while (fs2.Read(b, 0, b.Length) > 0) { Console.WriteLine(utf8.GetString(b)); } fs2.Close(); } } } | cs |
[C#] Stream 클래스 (0) | 2018.06.07 |
---|---|
[C#] 스트림 (0) | 2018.06.07 |
[C#] 사용자 예외처리 (0) | 2018.05.31 |
[C#] Exception, throw (0) | 2018.05.31 |
[C#] 예외 처리 finally (0) | 2018.05.31 |
표준 입출력을 처리하기 위한 클래스
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace StreamTest { class StreamTest { static void Main(string[] args) { int ch; Stream st = Console.OpenStandardInput(); Console.WriteLine("CanRead:" + st.CanRead); Console.WriteLine("끝내시려면 'S'를 입력해 주세요"); Console.Write("아무 글이나 입력하시고 Enter를 쳐주세요"); while ((ch = st.ReadByte()) != -1) { Console.WriteLine("ch의 값 테스트: "+ch); if (ch == 83) { Console.WriteLine("stop"); return; } Console.WriteLine((char)ch + ":" + ch); } } } } | cs |
[C#] FileStream (0) | 2018.06.07 |
---|---|
[C#] 스트림 (0) | 2018.06.07 |
[C#] 사용자 예외처리 (0) | 2018.05.31 |
[C#] Exception, throw (0) | 2018.05.31 |
[C#] 예외 처리 finally (0) | 2018.05.31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.IO; namespace ConsoleApplication19 { class FileTest { static void Main(string[] args) { Console.WriteLine("1.프로그램 시작"); File.Copy("FileTest.cs", "Output.txt",true); Console.WriteLine("2. ===Copied Successfullyu!!==="); bool exist = File.Exists("./Output.txt"); Console.WriteLine("3.Output.txt 존재여부:"+ exist); DateTime dt = File.GetCreationTime("./Output.txt"); Console.WriteLine("4.파일 생성시간:" + dt.ToString()); Console.WriteLine("5.프로그램 끝"); } } } | cs |
파일스트림
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; using System.IO; namespace FileTest2 { class FileTest2 { static void Main(string[] args) { FileStream fs = File.OpenRead("./hero1.txt"); //문자로 전환 StreamReader r = new StreamReader(fs, System.Text.Encoding.Default); // r.BaseStream.Seek(0,SeekOrigin.Begin); while (r.Peek() > -1) { Console.WriteLine(r.ReadLine()); } r.Close(); } } } | cs |
[C#] FileStream (0) | 2018.06.07 |
---|---|
[C#] Stream 클래스 (0) | 2018.06.07 |
[C#] 사용자 예외처리 (0) | 2018.05.31 |
[C#] Exception, throw (0) | 2018.05.31 |
[C#] 예외 처리 finally (0) | 2018.05.31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace UserExceptionTest { class UserException : ApplicationException{ private string nickname; public UserException(): base(){} public UserException(string str, string nickname): base(str) { this.nickname = nickname; } public string GetUserName() { return nickname; } public override string ToString() { return "메세지:"+ Message; } } class UserExceptionTest { static void Main(string[] args) { try { throw new UserException(" 에러가 발생했습니다.", "jabook"); } catch (UserException e) { Console.WriteLine("1.Message속성 :" + e.Message); Console.WriteLine("2.e,StackTrace 속성 :" + e.StackTrace); Console.WriteLine("3. ToString() 속성 :" + e.ToString()); Console.WriteLine("4.GetUserName속성 :" + e.GetUserName()); } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | using System; namespace NewExceptionTest2 { public class Top{ public void SayHellow() { try { string s = null; Console.WriteLine(s.ToString()); } catch { throw new Exception("다시 날립니다."); } } } class NewExceptionTest2 { static void Main(string[] args) { Console.WriteLine("1.프로그램시작"); Top t = new Top(); try { t.SayHellow(); } catch (Exception e) { Console.WriteLine("2. message 속성 정보: " + e.Message); Console.WriteLine("3. " + e.ToString()); } Console.WriteLine("4.프로그램종료"); } } } | cs |
[C#] Stream 클래스 (0) | 2018.06.07 |
---|---|
[C#] 스트림 (0) | 2018.06.07 |
[C#] Exception, throw (0) | 2018.05.31 |
[C#] 예외 처리 finally (0) | 2018.05.31 |
[C#] 다중 catch (0) | 2018.05.31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace NewExceptionTest { class NewExceptionTest { static void Main(string[] args) { Console.WriteLine("1. Start"); try { throw new BadImageFormatException(); // err } catch(Exception e) { Console.WriteLine("2. 예외 정보 출력"); Console.WriteLine(e); } Console.WriteLine("3.종료"); } } } | cs |
throw는 사용자가 직접 예외를 만들어서 발생시킬 때 사용하는 키워드.
[C#] 스트림 (0) | 2018.06.07 |
---|---|
[C#] 사용자 예외처리 (0) | 2018.05.31 |
[C#] 예외 처리 finally (0) | 2018.05.31 |
[C#] 다중 catch (0) | 2018.05.31 |
[C#] 예외처리 try catch 구문 (0) | 2018.05.24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FinallyTest { class FinallyTest { static void Main(string[] args) { int[] exArray = new int[2]; try { exArray[0] = 0; exArray[1] = 1; for (int i = 0; i < exArray.Length; i++) { Console.WriteLine("exArray[" + i + "] = " + exArray[i]); } } catch (IndexOutOfRangeException e) { Console.WriteLine(e); } finally { Console.WriteLine("첫번째 finally 문 입니다."); Console.WriteLine(); } try { exArray[2] = 2; // err Console.WriteLine("exArray[2] = " + exArray[2]); } catch (IndexOutOfRangeException e) { Console.WriteLine(e); } finally { Console.WriteLine("두번째 finally 문 입니다. "); } } } } | cs |
finally문은 err 발생처리와 상관없이 반드시 처리되는 구문 이다.
[C#] 사용자 예외처리 (0) | 2018.05.31 |
---|---|
[C#] Exception, throw (0) | 2018.05.31 |
[C#] 다중 catch (0) | 2018.05.31 |
[C#] 예외처리 try catch 구문 (0) | 2018.05.24 |
[C#] NameValueCollection 클래스 (0) | 2018.05.24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; namespace MultiCatchTest { class MultiCatchTest { static void Main(string[] args) { int[] a = { 1, 11, 22, 33 }; int[] b = { 0, 1, 2 }; for (int y = 0; y < a.Length; y++ ) { try { Console.WriteLine(a[y] + "/" + b[y] + "=" + a[y] / b[y]); } catch (DivideByZeroException) { Console.WriteLine("0으로 나눌 수 없습니다."); } catch (Exception e) { Console.WriteLine(e); } } Console.WriteLine(" 끝 "); } } } | cs |
최상위 예외 클래스인 exception e 를 더 위로 옮기면 컴파일 에러가 발생한다.
왜냐하면 , 0나누기 에러가 최상위가 포함하므로, 다중 예외처리를 할때는
하위 타입부터 기술 해야한다
[C#] Exception, throw (0) | 2018.05.31 |
---|---|
[C#] 예외 처리 finally (0) | 2018.05.31 |
[C#] 예외처리 try catch 구문 (0) | 2018.05.24 |
[C#] NameValueCollection 클래스 (0) | 2018.05.24 |
[C#] Stack (0) | 2018.05.24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | using System; using System.Collections; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication18 { class TryCatchTest { static void Main(string[] args) { string str = null; try { Console.WriteLine(str.ToString()); } catch (NullReferenceException e) { Console.WriteLine(e.ToString() ); } Console.WriteLine("프로그램을 마칩니다."); } } } | cs |
[C#] 예외 처리 finally (0) | 2018.05.31 |
---|---|
[C#] 다중 catch (0) | 2018.05.31 |
[C#] NameValueCollection 클래스 (0) | 2018.05.24 |
[C#] Stack (0) | 2018.05.24 |
[C#] Hashtable 클래스 (0) | 2018.05.24 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | using System; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication17 { class NameValueCollectionTest { static void Main(string[] args) { NameValueCollection dic = new NameValueCollection(); dic.Set("이름","김길동"); dic.Set("나이", "24"); dic.Set("성별", "남"); string name = dic.Get("이름"); string age = dic.Get("나이"); string sex = dic.Get("성별"); Console.WriteLine("이름 ={0}, 나이 ={1}, 성별 ={2}", name, age, sex); for (int i = 0; i < dic.Count; i++) Console.Write("{0}= {1}\n", dic.Keys[i], dic[i]); } } } | cs |
[C#] 다중 catch (0) | 2018.05.31 |
---|---|
[C#] 예외처리 try catch 구문 (0) | 2018.05.24 |
[C#] Stack (0) | 2018.05.24 |
[C#] Hashtable 클래스 (0) | 2018.05.24 |
[C#] Length와 Rank 속성 (0) | 2018.05.20 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | using System; using System.Collections; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication16 { class StackTest { static void Main(string[] args) { Stack stack = new Stack(); stack.Push("Hi"); stack.Push("Hello"); stack.Push("World"); stack.Push("7777"); Print("1. stack의 목록:",stack); object obj = stack.Pop(); Console.WriteLine("2.pop:{0}", obj); obj = stack.Pop(); Console.WriteLine("3.pop:{0}", obj); Print("4. pop을 두번한 후의 목록:", stack); obj = stack.Peek(); Console.WriteLine("5. Peek: {0}",obj); } public static void Print(string info, IEnumerable myCollection) { Console.Write(info + "\n\t"); IEnumerator myEnumerator = myCollection.GetEnumerator(); while (myEnumerator.MoveNext()) Console.Write("{0}, ", myEnumerator.Current); Console.WriteLine(); } } } | cs |
1. stack의 목록:
7777, World, Hello, Hi,
2.pop:7777
3.pop:World
4. pop을 두번한 후의 목록:
Hello, Hi,
5. Peek: Hello
계속하려면 아무 키나 누르십시오 . . .
[C#] 예외처리 try catch 구문 (0) | 2018.05.24 |
---|---|
[C#] NameValueCollection 클래스 (0) | 2018.05.24 |
[C#] Hashtable 클래스 (0) | 2018.05.24 |
[C#] Length와 Rank 속성 (0) | 2018.05.20 |
[C#] Clone(), Array.Copy배열 복제 (0) | 2018.05.20 |