class Program
{
static void Main(string[] args)
{
Console.ForegroundColor = ConsoleColor.White;
//第一题:
Capture.CaptureEx();
//第二题:
//字符的写入和访问
//Chars.Write();
//Chars.Read();
//字节的写入和访问
//Bytes.Write();
//Bytes.Read();
//二进制的写入和访问
//BinarySystem.Write();
//BinarySystem.Reader();
Console.ReadKey();
}
}
/// <summary>
/// 捕获异常
/// </summary>
class Capture
{
/// <summary>
/// 捕获异常在当前目录记录详细错误
/// </summary>
public static void CaptureEx()
{
//语言格式
Encoding e = Encoding.GetEncoding("GB2312");
//在当前目录下存储ExceptionInfo.txt的文件存储报错
string path = Path.Combine(Environment.CurrentDirectory, "ExceptionInfo.txt");
//用字符串转换,并且不覆盖,用gb2312格式的语言
StreamWriter writer = new StreamWriter(path, true, e);
try
{
Console.Write("请输入一个数值类型的内容,输入其他内容报出异常");
int i = Convert.ToInt32(Console.ReadLine());
}
catch (Exception ex)
{
//声明一个时间对象,用于存储现在的时间
DateTime dlt = DateTime.Now;
//传入详细的异常信息
writer.WriteLine(dlt + " " + ex.ToString() + "\n");
}
finally
{
//清除缓存
writer.Flush();
//关闭通道对象
writer.Close();
}
//输出内容对象
StreamReader reader = new StreamReader(path, e);
//输出全部字符
Console.WriteLine(reader.ReadToEnd());
//关闭通道对象
reader.Close();
}
}
/// <summary>
/// 字符流读写内容
/// </summary>
class Chars
{
/// <summary>
/// 字符流写入
/// </summary>
public static void Write()
{
//语言格式
Encoding e = Encoding.GetEncoding("GB2312");
//在当前目录下存储Chars.txt的文件
string path = Path.Combine(Environment.CurrentDirectory, "Chars.txt");
//用字符串转换,并且不覆盖,用gb2312格式的语言
StreamWriter writer = new StreamWriter(path,true, e);
Console.Write("请输入一段字符:");
//接收输入的内容
string vessel = Console.ReadLine();
//向文件中传入内容
writer.Write(vessel);
//清除缓存
writer.Flush();
//关闭通道对象
writer.Close();
}
/// <summary>
/// 字符流访问
/// </summary>
public static void Read()
{
//语言格式
Encoding e = Encoding.GetEncoding("GB2312");
//在当前目录下存储Chars.txt的文件
string path = Path.Combine(Environment.CurrentDirectory, "Chars.txt");
//输出内容对象
StreamReader reader = new StreamReader(path, e);
//输出全部字符
Console.WriteLine(reader.ReadToEnd());
//关闭通道对象
reader.Close();
}
}
/// <summary>
/// 字节流读写内容
/// </summary>
class Bytes
{
/// <summary>
/// 字节的写入
/// </summary>
public static void Write()
{
//在当前目录下存储Bytes.txt的文件
string path = Path.Combine(Environment.CurrentDirectory, "Bytes.txt");
//字节类对象
Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
try
{
Console.Write("请输入一个字节:");
//接收字节内容
byte j = Convert.ToByte(Console.ReadLine());
//传入所有内容
stream.WriteByte(j);
//清除缓存
stream.Flush();
//关闭通道对象
stream.Close();
}
catch
{
//清除缓存
stream.Flush();
//关闭通道对象
stream.Close();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("输入内容非字节,请重新输入!");
Console.ForegroundColor = ConsoleColor.White;
//重新执行此命令
Write();
}
}
/// <summary>
/// 字节的访问
/// </summary>
public static void Read()
{
//在当前目录下存储Bytes.txt的文件
string path = Path.Combine(Environment.CurrentDirectory, "Bytes.txt");
//字节类对象
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
int D;
//输出全部内容
while ((D = stream.ReadByte()) >= 0)
{
Console.WriteLine(D);
}
}
}
/// <summary>
/// 二进制的读写内容
/// </summary>
class BinarySystem
{
/// <summary>
/// 二进制的写入内容
/// </summary>
public static void Write()
{
//字符类型
Encoding e = Encoding.GetEncoding("GB2312");
//当前位置创建BinarySystem.txt文件
string path = Path.Combine(Environment.CurrentDirectory, "BinarySystem.txt");
//创建字节流stream对象,提供创建对象的位置,并且是写入
Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
//创建二进制BinaryWriter对象,传入对象和字符类型;
BinaryWriter binW = new BinaryWriter(stream, e);
Console.Write("请输入一段字符串:");
//输入的字符串
string vessel = Console.ReadLine();
//将输入的内容写入到指定的文件中
binW.Write(vessel);
//清除所有输入
binW.Flush();
//关闭启用的对象
stream.Close();
//关闭启用的对象
binW.Close();
}
/// <summary>
/// 二进制的读出内容
/// </summary>
public static void Reader()
{
//字符类型
Encoding e = Encoding.GetEncoding("GB2312");
//当前位置创建BinarySystem.txt文件
string path = Path.Combine(Environment.CurrentDirectory, "BinarySystem.txt");
//创建字节流stream对象,提供创建对象的位置,并且是打开,读取内容
Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
//创建二进制BinaryReader对象,传入对象和字符类型;
BinaryReader binR = new BinaryReader(stream, e);
//获取全部内容
Console.WriteLine(binR.ReadString());
//关闭启用的对象
binR.Close();
}
}