filereaddemo.cs

来自「北大青鸟内部资料」· CS 代码 · 共 43 行

CS
43
字号
using System;
using System.IO;
using System.Text;

class FileReadDemo
{
	public static void Main() 
	{

		string path;
		Console.WriteLine ("请输入要读取的文件名。 指定带路径的完整名称:");
		path = Console.ReadLine ();

		try
		{
			if (!File.Exists(path))
			{
				Console.WriteLine("文件不存在");
			}
			else
			{
		
				// 打开流以进行读取。
				FileStream fs = File.OpenRead(path); 
		
				//创建一个 byte 数组以读取数据 
				byte[] arr = new byte[100];
				UTF8Encoding data = new UTF8Encoding(true);
        
				//继续读完文件中的所有数据
				while (fs.Read(arr,0,arr.Length) > 0) 
				{
					Console.WriteLine(data.GetString(arr));
				}
			}
		}
		catch(Exception ex)
		{
			Console.WriteLine("出现错误: " + ex.Message);
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?