class1.cs

来自「原代码详细说明是关于c++方面的希望可以帮助大家使用」· CS 代码 · 共 55 行

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

class Test 
{
    
	public static void Main() 
	{
		string path = @"c:\MyTest.txt";

		//如果文件存在,先删除 
		if (File.Exists(path)) 
		{
			File.Delete(path);
		}

		//创建文件
		using (FileStream fs = File.Create(path)) 
		{
			AddText(fs, "This is some text");
			AddText(fs, "This is some more text,");
			AddText(fs, "\r\nand this is on a new line");
			AddText(fs, "\r\n\r\nThe following is a subset of numbers:\r\n");

			for (int i=1;i <=100;i++) 
			{
				AddText(fs, i+" ");
				if (i%10 == 0) //10个一行
				{
					AddText(fs, "\r\n");
				}
			}
		}

		//读取文件.
		using (FileStream fs = File.OpenRead(path)) 
		{
			byte[] b = new byte[1024];
			UTF8Encoding temp = new UTF8Encoding(true);
			while (fs.Read(b,0,b.Length) > 0) 
			{
				Console.WriteLine(temp.GetString(b));
			}
		}
	}

	private static void AddText(FileStream fs, string value) 
	{
		byte[] info = new UTF8Encoding(true).GetBytes(value);
		fs.Write(info, 0, info.Length);
	}
}

⌨️ 快捷键说明

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