Eric Yih's Blog

Do what you like, like what you do.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Using StreamReader and StreamWriter

Posted on 2008-12-14 22:56  Eric Yih  阅读(391)  评论(0)    收藏  举报
   1:  /*
   2:   * Created by SharpDevelop.
   3:   * User: Eric Yih
   4:   * Date: 2008-12-14
   5:   * Time: 16:45
   6:   * 
   7:   * To change this template use Tools | Options | Coding | Edit Standard Headers.
   8:   
*/
   
9:  using System;
  
10:  using System.IO;
  
11:  using System.Collections;
  
12:  using System.Diagnostics;
  
13:   
  
14:  namespace Studying.MyStream
  
15:  {
  
16:      class MyStream
  
17:      {
  
18:          public void ReadFile(string filepath)
  
19:          {
  
20:              try
  
21:              {
  
22:                  StreamReader sr = new StreamReader(filepath);
  
23:   
  
24:                  string sLine = "";
  
25:                  ArrayList arrText = new ArrayList();
  
26:                  while(sLine != null)
  
27:                  {
  
28:                      sLine = sr.ReadLine();
  
29:                      if (sLine != null)
  
30:                      {
  
31:                          arrText.Add(sLine);
  
32:                      }
  
33:                  }
  
34:                  sr.Close();
  
35:                  
  
36:                  foreach(string output in arrText)
  
37:                  {
  
38:                      Console.WriteLine(output);
  
39:                  }
  
40:              }
  
41:              catch(Exception ex)
  
42:              {
  
43:                  throw new Exception(ex.Message);
  
44:              }
  
45:          }
  
46:          
  
47:          public void WriteFile(string username, string password)
  
48:          {
  
49:              try
  
50:              {
  
51:                  //Initialize StreamWriter as a Object "sw", and specify the file name and directory.
  52:                  StreamWriter sw = new StreamWriter(System.Environment.CurrentDirectory + "\\userinfo.bat");
  
53:   
  
54:                  sw.WriteLine(username);
  
55:                  sw.WriteLine(password);
  
56:   
  
57:                  //Close Stream
  58:                  sw.Close();
  
59:              }
  
60:              catch(Exception ex)
  
61:              {
  
62:                  throw new Exception(ex.Message);
  
63:              }
  
64:          }
  
65:          
  
66:          public static void Main(string[] args)
  
67:          {
  
68:              String FilePath = System.Environment.CurrentDirectory + "\\userinfo.bat";
  
69:   
  
70:              //Check if the file exist on the current directory
  71:              if(File.Exists(FilePath))
  
72:              {
  
73:                  File.Delete(System.Environment.CurrentDirectory + "\\userinfo.bat");
  
74:              }
  
75:              
  
76:              MyStream myStream = new MyStream();
  
77:              
  
78:              myStream.WriteFile("root","password");
  
79:              
  
80:              myStream.ReadFile(FilePath);
  
81:              
  
82:              if(File.Exists(FilePath))
  
83:              {
  
84:                  File.Delete(System.Environment.CurrentDirectory + "\\userinfo.bat");
  
85:              }
  
86:          }
  
87:      }
  
88:  }