////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Main(string[] args){ //读文件代码 byte[]m_bDataRead = new byte[100]; char[]m_cDataRead = new char[100]; try { FileStreamm_FileStream = new FileStream(@"c:\file.txt", FileMode.Open); m_FileStream.Seek(0, SeekOrigin.Begin); m_FileStream.Read(m_bDataRead,0, 100); } catch(IOException ex) { Console.WriteLine("There is an IOException"); Console.WriteLine(ex.Message); Console.ReadLine(); return; } //用UTF8编码将字节数组转换为字符数组 Decoderm_Dec = Encoding.UTF8.GetDecoder(); m_Dec.GetChars(m_bDataRead, 0,m_bDataRead.Length, m_cDataRead, 0); Console.WriteLine("Read From File Succeed!"); Console.WriteLine(m_cDataRead); Console.ReadLine(); return; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Main(string[] args)
{
try
{
StreamWriter m_sw = new StreamWriter(@"c:\file.txt", true);
m_sw.WriteLine("Another File Operation Method");
m_sw.Close();
}
catch (IOException ex)
{
Console.WriteLine(ex);
return;
}
Console.WriteLine("Write to File Succeed!");
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void Main(string[] args)
{
try
{
StreamReader m_sr = new StreamReader(@"c:\file.txt");
string m_data = m_sr.ReadToEnd();
Console.WriteLine(m_data);
m_sr.Close();
}
catch (IOException ex)
{
Console.WriteLine(ex);
return;
}
Console.WriteLine("Write to File Succeed!");
}
|