{"id":243356,"date":"2022-06-02T09:27:44","date_gmt":"2022-06-02T01:27:44","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=243356"},"modified":"2022-05-23T14:29:35","modified_gmt":"2022-05-23T06:29:35","slug":"understand-the-input","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/understand-the-input.html","title":{"rendered":"\u4e86\u89e3\u4e0bC# \u6587\u4ef6\u7684\u8f93\u5165\u4e0e\u8f93\u51fa"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u4e00\u4e2a\u6587\u4ef6\u662f\u4e00\u4e2a\u5b58\u50a8\u5728\u78c1\u76d8\u4e2d\u5e26\u6709\u6307\u5b9a\u540d\u79f0\u548c\u76ee\u5f55\u8def\u5f84\u7684\u6570\u636e\u96c6\u5408\u3002\u5f53\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u65f6\uff0c\u5b83\u53d8\u6210\u4e00\u4e2a\u6d41\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u4ece\u6839\u672c\u4e0a\u8bf4\uff0c\u6d41\u662f\u901a\u8fc7\u901a\u4fe1\u8def\u5f84\u4f20\u9012\u7684\u5b57\u8282\u5e8f\u5217\u3002\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u6d41\uff1a\u8f93\u5165\u6d41 \u548c \u8f93\u51fa\u6d41\u3002\u8f93\u5165\u6d41\u7528\u4e8e\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff08\u8bfb\u64cd\u4f5c\uff09\uff0c\u8f93\u51fa\u6d41\u7528\u4e8e\u5411\u6587\u4ef6\u5199\u5165\u6570\u636e\uff08\u5199\u64cd\u4f5c\uff09\u3002<\/p>\n

C# I\/O \u7c7b<\/strong><\/div>\n

System.IO \u547d\u540d\u7a7a\u95f4\u6709\u5404\u79cd\u4e0d\u540c\u7684\u7c7b\uff0c\u7528\u4e8e\u6267\u884c\u5404\u79cd\u6587\u4ef6\u64cd\u4f5c\uff0c\u5982\u521b\u5efa\u548c\u5220\u9664\u6587\u4ef6\u3001\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\uff0c\u5173\u95ed\u6587\u4ef6\u7b49\u3002<\/p>\n

\u4e0b\u8868\u5217\u51fa\u4e86\u4e00\u4e9b System.IO \u547d\u540d\u7a7a\u95f4\u4e2d\u5e38\u7528\u7684\u975e\u62bd\u8c61\u7c7b\uff1a<\/p>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
I\/O \u7c7b<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
BinaryReader<\/td>\n\u4ece\u4e8c\u8fdb\u5236\u6d41\u8bfb\u53d6\u539f\u59cb\u6570\u636e\u3002<\/td>\n<\/tr>\n
BinaryWriter<\/td>\n\u4ee5\u4e8c\u8fdb\u5236\u683c\u5f0f\u5199\u5165\u539f\u59cb\u6570\u636e\u3002<\/td>\n<\/tr>\n
BufferedStream<\/td>\n\u5b57\u8282\u6d41\u7684\u4e34\u65f6\u5b58\u50a8\u3002<\/td>\n<\/tr>\n
Directory<\/td>\n\u6709\u52a9\u4e8e\u64cd\u4f5c\u76ee\u5f55\u7ed3\u6784\u3002<\/td>\n<\/tr>\n
DirectoryInfo<\/td>\n\u7528\u4e8e\u5bf9\u76ee\u5f55\u6267\u884c\u64cd\u4f5c\u3002<\/td>\n<\/tr>\n
DriveInfo<\/td>\n\u63d0\u4f9b\u9a71\u52a8\u5668\u7684\u4fe1\u606f\u3002<\/td>\n<\/tr>\n
File<\/td>\n\u6709\u52a9\u4e8e\u5904\u7406\u6587\u4ef6\u3002<\/td>\n<\/tr>\n
FileInfo<\/td>\n\u7528\u4e8e\u5bf9\u6587\u4ef6\u6267\u884c\u64cd\u4f5c\u3002<\/td>\n<\/tr>\n
FileStream<\/td>\n\u7528\u4e8e\u6587\u4ef6\u4e2d\u4efb\u4f55\u4f4d\u7f6e\u7684\u8bfb\u5199\u3002<\/td>\n<\/tr>\n
MemoryStream<\/td>\n\u7528\u4e8e\u968f\u673a\u8bbf\u95ee\u5b58\u50a8\u5728\u5185\u5b58\u4e2d\u7684\u6570\u636e\u6d41\u3002<\/td>\n<\/tr>\n
Path<\/td>\n\u5bf9\u8def\u5f84\u4fe1\u606f\u6267\u884c\u64cd\u4f5c\u3002<\/td>\n<\/tr>\n
StreamReader<\/td>\n\u7528\u4e8e\u4ece\u5b57\u8282\u6d41\u4e2d\u8bfb\u53d6\u5b57\u7b26\u3002<\/td>\n<\/tr>\n
StreamWriter<\/td>\n\u7528\u4e8e\u5411\u4e00\u4e2a\u6d41\u4e2d\u5199\u5165\u5b57\u7b26\u3002<\/td>\n<\/tr>\n
StringReader<\/td>\n\u7528\u4e8e\u8bfb\u53d6\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u3002<\/td>\n<\/tr>\n
StringWriter<\/td>\n\u7528\u4e8e\u5199\u5165\u5b57\u7b26\u4e32\u7f13\u51b2\u533a\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
FileStream \u7c7b<\/strong><\/div>\n

System.IO \u547d\u540d\u7a7a\u95f4\u4e2d\u7684 FileStream \u7c7b\u6709\u52a9\u4e8e\u6587\u4ef6\u7684\u8bfb\u5199\u4e0e\u5173\u95ed\u3002\u8be5\u7c7b\u6d3e\u751f\u81ea\u62bd\u8c61\u7c7b Stream\u3002<\/p>\n

\u60a8\u9700\u8981\u521b\u5efa\u4e00\u4e2a FileStream \u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff0c\u6216\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u4ef6\u3002\u521b\u5efa FileStream \u5bf9\u8c61\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

FileStream<\/span> <\/span><object_name><\/span> <\/span>=<\/span> <\/span>new<\/span> <\/span>FileStream<\/span>(<\/span> <\/span><file_name><\/span>,<\/span>\r\n<\/span><<\/span>FileMode<\/span> <\/span>Enumerator<\/span>>,<\/span> <\/span><<\/span>FileAccess<\/span> <\/span>Enumerator<\/span>>,<\/span> <\/span><<\/span>FileShare<\/span> <\/span>Enumerator<\/span>>);<\/span><\/pre>\n

\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a FileStream \u5bf9\u8c61 F \u6765\u8bfb\u53d6\u540d\u4e3a sample.txt \u7684\u6587\u4ef6\uff1a<\/p>\n

FileStream<\/span> F <\/span>=<\/span> <\/span>new<\/span> <\/span>FileStream<\/span>(<\/span>\"sample.txt\"<\/span>,<\/span> <\/span>FileMode<\/span>.<\/span>Open<\/span>,<\/span> <\/span>FileAccess<\/span>.<\/span>Read<\/span>,<\/span> <\/span>FileShare<\/span>.<\/span>Read<\/span>);<\/span><\/pre>\n\n\n\n\n\n\n
\u53c2\u6570<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
FileMode<\/td>\n\n

FileMode<\/b> \u679a\u4e3e\u5b9a\u4e49\u4e86\u5404\u79cd\u6253\u5f00\u6587\u4ef6\u7684\u65b9\u6cd5\u3002FileMode \u679a\u4e3e\u7684\u6210\u5458\u6709\uff1a<\/p>\n

    \n
  • Append<\/b>\uff1a\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u5149\u6807\u653e\u7f6e\u5728\u6587\u4ef6\u7684\u672b\u5c3e\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u6587\u4ef6\u3002<\/li>\n
  • Create<\/b>\uff1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u5220\u9664\u65e7\u6587\u4ef6\uff0c\u7136\u540e\u521b\u5efa\u65b0\u6587\u4ef6\u3002<\/li>\n
  • CreateNew<\/b>\uff1a\u6307\u5b9a\u64cd\u4f5c\u7cfb\u7edf\u5e94\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u5df2\u5b58\u5728\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n
  • Open<\/b>\uff1a\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n
  • OpenOrCreate<\/b>\uff1a\u6307\u5b9a\u64cd\u4f5c\u7cfb\u7edf\u5e94\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u7528\u6307\u5b9a\u7684\u540d\u79f0\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u6253\u5f00\u3002<\/li>\n
  • Truncate<\/b>\uff1a\u6253\u5f00\u4e00\u4e2a\u5df2\u6709\u7684\u6587\u4ef6\uff0c\u6587\u4ef6\u4e00\u65e6\u6253\u5f00\uff0c\u5c31\u5c06\u88ab\u622a\u65ad\u4e3a\u96f6\u5b57\u8282\u5927\u5c0f\u3002\u7136\u540e\u6211\u4eec\u53ef\u4ee5\u5411\u6587\u4ef6\u5199\u5165\u5168\u65b0\u7684\u6570\u636e\uff0c\u4f46\u662f\u4fdd\u7559\u6587\u4ef6\u7684\u521d\u59cb\u521b\u5efa\u65e5\u671f\u3002\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n
FileAccess<\/td>\n\n

FileAccess<\/b> \u679a\u4e3e\u7684\u6210\u5458\u6709\uff1aRead<\/b>\u3001ReadWrite<\/b> \u548c Write<\/b>\u3002<\/p>\n<\/td>\n<\/tr>\n

FileShare<\/td>\n\n

FileShare<\/b> \u679a\u4e3e\u7684\u6210\u5458\u6709\uff1a<\/p>\n

    \n
  • Inheritable<\/b>\uff1a\u5141\u8bb8\u6587\u4ef6\u53e5\u67c4\u53ef\u7531\u5b50\u8fdb\u7a0b\u7ee7\u627f\u3002Win32 \u4e0d\u76f4\u63a5\u652f\u6301\u6b64\u529f\u80fd\u3002<\/li>\n
  • None<\/b>\uff1a\u8c22\u7edd\u5171\u4eab\u5f53\u524d\u6587\u4ef6\u3002\u6587\u4ef6\u5173\u95ed\u524d\uff0c\u6253\u5f00\u8be5\u6587\u4ef6\u7684\u4efb\u4f55\u8bf7\u6c42\uff08\u7531\u6b64\u8fdb\u7a0b\u6216\u53e6\u4e00\u8fdb\u7a0b\u53d1\u51fa\u7684\u8bf7\u6c42\uff09\u90fd\u5c06\u5931\u8d25\u3002 <\/li>\n
  • Read<\/b>\uff1a\u5141\u8bb8\u968f\u540e\u6253\u5f00\u6587\u4ef6\u8bfb\u53d6\u3002\u5982\u679c\u672a\u6307\u5b9a\u6b64\u6807\u5fd7\uff0c\u5219\u6587\u4ef6\u5173\u95ed\u524d\uff0c\u4efb\u4f55\u6253\u5f00\u8be5\u6587\u4ef6\u4ee5\u8fdb\u884c\u8bfb\u53d6\u7684\u8bf7\u6c42\uff08\u7531\u6b64\u8fdb\u7a0b\u6216\u53e6\u4e00\u8fdb\u7a0b\u53d1\u51fa\u7684\u8bf7\u6c42\uff09\u90fd\u5c06\u5931\u8d25\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u6307\u5b9a\u4e86\u6b64\u6807\u5fd7\uff0c\u4ecd\u53ef\u80fd\u9700\u8981\u9644\u52a0\u6743\u9650\u624d\u80fd\u591f\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002<\/li>\n
  • ReadWrite<\/b>\uff1a\u5141\u8bb8\u968f\u540e\u6253\u5f00\u6587\u4ef6\u8bfb\u53d6\u6216\u5199\u5165\u3002\u5982\u679c\u672a\u6307\u5b9a\u6b64\u6807\u5fd7\uff0c\u5219\u6587\u4ef6\u5173\u95ed\u524d\uff0c\u4efb\u4f55\u6253\u5f00\u8be5\u6587\u4ef6\u4ee5\u8fdb\u884c\u8bfb\u53d6\u6216\u5199\u5165\u7684\u8bf7\u6c42\uff08\u7531\u6b64\u8fdb\u7a0b\u6216\u53e6\u4e00\u8fdb\u7a0b\u53d1\u51fa\uff09\u90fd\u5c06\u5931\u8d25\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u6307\u5b9a\u4e86\u6b64\u6807\u5fd7\uff0c\u4ecd\u53ef\u80fd\u9700\u8981\u9644\u52a0\u6743\u9650\u624d\u80fd\u591f\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002 <\/li>\n
  • Write<\/b>\uff1a\u5141\u8bb8\u968f\u540e\u6253\u5f00\u6587\u4ef6\u5199\u5165\u3002\u5982\u679c\u672a\u6307\u5b9a\u6b64\u6807\u5fd7\uff0c\u5219\u6587\u4ef6\u5173\u95ed\u524d\uff0c\u4efb\u4f55\u6253\u5f00\u8be5\u6587\u4ef6\u4ee5\u8fdb\u884c\u5199\u5165\u7684\u8bf7\u6c42\uff08\u7531\u6b64\u8fdb\u7a0b\u6216\u53e6\u4e00\u8fdb\u8fc7\u7a0b\u53d1\u51fa\u7684\u8bf7\u6c42\uff09\u90fd\u5c06\u5931\u8d25\u3002\u4f46\u662f\uff0c\u5373\u4f7f\u6307\u5b9a\u4e86\u6b64\u6807\u5fd7\uff0c\u4ecd\u53ef\u80fd\u9700\u8981\u9644\u52a0\u6743\u9650\u624d\u80fd\u591f\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002 <\/li>\n
  • Delete<\/b>\uff1a\u5141\u8bb8\u968f\u540e\u5220\u9664\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
    \u5b9e\u4f8b<\/strong><\/div>\n

    \u4e0b\u9762\u7684\u7a0b\u5e8f\u6f14\u793a\u4e86 FileStream \u7c7b\u7684\u7528\u6cd5\uff1a<\/p>\n

    \u5b9e\u4f8b<\/p>\n

    using System;\r\nusing System.IO;\r\n\r\nnamespace FileIOApplication\r\n{\r\n    class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            FileStream F = new FileStream(\"test.dat\",\r\n            FileMode.OpenOrCreate, FileAccess.ReadWrite);\r\n\r\n            for (int i = 1; i <= 20; i++)\r\n            {\r\n                F.WriteByte((byte)i);\r\n            }\r\n\r\n            F.Position = 0;\r\n\r\n            for (int i = 0; i <= 20; i++)\r\n            {\r\n                Console.Write(F.ReadByte() + \" \");\r\n            }\r\n            F.Close();\r\n            Console.ReadKey();\r\n        }\r\n    }\r\n}<\/pre>\n

    \u5f53\u4e0a\u9762\u7684\u4ee3\u7801\u88ab\u7f16\u8bd1\u548c\u6267\u884c\u65f6\uff0c\u5b83\u4f1a\u4ea7\u751f\u4e0b\u5217\u7ed3\u679c\uff1a<\/p>\n

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1<\/pre>\n
    C# \u9ad8\u7ea7\u6587\u4ef6\u64cd\u4f5c<\/strong><\/div>\n

    \u4e0a\u9762\u7684\u5b9e\u4f8b\u6f14\u793a\u4e86 C# \u4e2d\u7b80\u5355\u7684\u6587\u4ef6\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8981\u5145\u5206\u5229\u7528 C# System.IO \u7c7b\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u60a8\u9700\u8981\u77e5\u9053\u8fd9\u4e9b\u7c7b\u5e38\u7528\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u3002<\/p>\n

    \u5728\u4e0b\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba\u8fd9\u4e9b\u7c7b\u548c\u5b83\u4eec\u6267\u884c\u7684\u64cd\u4f5c\u3002\u8bf7\u5355\u51fb\u94fe\u63a5\u8be6\u7ec6\u4e86\u89e3\u5404\u4e2a\u90e8\u5206\u7684\u77e5\u8bc6\uff1a<\/p>\n\n\n\n\n\n\n
    \u4e3b\u9898<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
    \u6587\u672c\u6587\u4ef6\u7684\u8bfb\u5199<\/a><\/td>\n\u5b83\u6d89\u53ca\u5230\u6587\u672c\u6587\u4ef6\u7684\u8bfb\u5199\u3002StreamReader<\/b> \u548c StreamWriter<\/b> \u7c7b\u6709\u52a9\u4e8e\u5b8c\u6210\u6587\u672c\u6587\u4ef6\u7684\u8bfb\u5199\u3002<\/td>\n<\/tr>\n
    \u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199<\/a><\/td>\n\u5b83\u6d89\u53ca\u5230\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u3002BinaryReader<\/b> \u548c BinaryWriter<\/b> \u7c7b\u6709\u52a9\u4e8e\u5b8c\u6210\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u3002<\/td>\n<\/tr>\n
    Windows \u6587\u4ef6\u7cfb\u7edf\u7684\u64cd\u4f5c<\/a><\/td>\n\u5b83\u8ba9 C# \u7a0b\u5e8f\u5458\u80fd\u591f\u6d4f\u89c8\u5e76\u5b9a\u4f4d Windows \u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"

    \u4ece\u6839\u672c\u4e0a\u8bf4\uff0c\u6d41\u662f\u901a\u8fc7\u901a\u4fe1\u8def\u5f84\u4f20\u9012\u7684\u5b57\u8282\u5e8f\u5217\u3002\u6709\u4e24\u4e2a\u4e3b\u8981\u7684\u6d41\uff1a\u8f93\u5165\u6d41 \u548c \u8f93\u51fa\u6d41\u3002\u8f93\u5165\u6d41\u7528\u4e8e\u4ece\u6587\u4ef6\u8bfb\u53d6\u6570\u636e\uff08\u8bfb\u64cd […]<\/p>\n","protected":false},"author":321,"featured_media":243360,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-243356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=243356"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243356\/revisions"}],"predecessor-version":[{"id":243362,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/243356\/revisions\/243362"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/243360"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=243356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=243356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=243356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}