{"id":202817,"date":"2025-05-22T09:19:07","date_gmt":"2025-05-22T01:19:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202817\/"},"modified":"2025-05-22T09:19:07","modified_gmt":"2025-05-22T01:19:07","slug":"java%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e7%9a%84%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e6%9c%ba%e5%88%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202817\/","title":{"rendered":"Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236<\/h1>\n<p>\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0cEither \u7c7b\u578b\u7528\u4e8e\u4f18\u96c5\u5904\u7406\u9519\u8bef\uff1aEither \u7c7b\u578b\u5c01\u88c5\u9519\u8bef\u6216\u6b63\u786e\u503c\uff0c\u907f\u514d\u5f02\u5e38\u5f15\u8d77\u4ee3\u7801\u6df7\u4e71\u3002\u63d0\u4f9b isLeft()\u3001getLeft()\u3001isRight()\u3001getRight() \u7b49\u65b9\u6cd5\u83b7\u53d6\u6216\u68c0\u67e5\u503c\u7c7b\u578b\u3002\u4f7f\u7528 flatMap \u53ef\u5e73\u5c55\u5d4c\u5957\u7684 Either\uff0c\u65b9\u4fbf\u5904\u7406\u5d4c\u5957\u9519\u8bef\u3002\u63d0\u5347\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u7b80\u5316\u9519\u8bef\u5904\u7406\u5e76\u907f\u514d\u5f02\u5e38\u6c61\u67d3\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729945166671cde4ebce44.jpg\" class=\"aligncenter\"><\/p>\n<h2>Java \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684\u9519\u8bef\u5904\u7406<\/h2>\n<p><strong>\u524d\u8a00<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u9519\u8bef\u5904\u7406\u81f3\u5173\u91cd\u8981\u3002\u4f20\u7edf\u4e0a\uff0cJava \u4f7f\u7528\u5f02\u5e38\u6765\u5904\u7406\u9519\u8bef\uff0c\u4f46\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u6df7\u4e71\u548c\u96be\u4ee5\u7ef4\u62a4\u3002\u51fd\u6570\u5f0f\u7f16\u7a0b\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\uff1a\u4f7f\u7528 Either \u7c7b\u578b\u3002<\/p>\n<p><strong>Either \u7c7b\u578b<\/strong><\/p>\n<p>Either \u7c7b\u578b\u662f\u4e00\u4e2a\u6cdb\u578b\u7c7b\u578b\uff0c\u6709\u4e24\u79cd\u53ef\u80fd\u7684\u503c\uff1a<\/p>\n<ul>\n<li><code>Left&lt;E&gt;<\/code>\uff1a\u5305\u542b\u4e00\u4e2a\u9519\u8bef\u503c<\/li>\n<li><code>Right&lt;T&gt;<\/code>\uff1a\u5305\u542b\u4e00\u4e2a\u6b63\u786e\u503c<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u6587\u4ef6\u8bfb\u53d6<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u8bfb\u53d6\u6587\u4ef6\u5e76\u5c06\u5176\u5185\u5bb9\u4f5c\u4e3a\u5b57\u7b26\u4e32\u8fd4\u56de\u7684\u51fd\u6570\uff1a<\/p>\n<pre>public static String readFile(String filePath) throws IOException {\n  try {\n    return Files.readString(Paths.get(filePath));\n  } catch (IOException e) {\n    throw new IOException(\"Error reading file: \" + e.getMessage());\n  }\n}<\/pre>\n<p>\u4f7f\u7528 Either\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u9519\u8bef\u5c01\u88c5\u5230 Left \u5b9e\u4f8b\u4e2d\uff1a<\/p>\n<pre>public static Either&lt;IOException, String&gt; readFileEither(String filePath) {\n  try {\n    return Either.right(Files.readString(Paths.get(filePath)));\n  } catch (IOException e) {\n    return Either.left(new IOException(\"Error reading file: \" + e.getMessage()));\n  }\n}<\/pre>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<p>Either \u7c7b\u578b\u63d0\u4f9b\u4e86\u51e0\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u9519\u8bef\uff1a<\/p>\n<ul>\n<li><code>isLeft()<\/code>\uff1a\u68c0\u67e5 Either \u662f\u5426\u4e3a Left \u7c7b\u578b<\/li>\n<li><code>getLeft()<\/code>\uff1a\u83b7\u53d6 Left \u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u9519\u8bef\u503c<\/li>\n<li><code>isRight()<\/code>\uff1a\u68c0\u67e5 Either \u662f\u5426\u4e3a Right \u7c7b\u578b<\/li>\n<li><code>getRight()<\/code>\uff1a\u83b7\u53d6 Right \u5b9e\u4f8b\u4e2d\u5305\u542b\u7684\u6b63\u786e\u503c<\/li>\n<\/ul>\n<p><strong>\u4f7f\u7528 flatMap \u5904\u7406\u5d4c\u5957 Either<\/strong><\/p>\n<p>\u5728\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\uff0c\u5d4c\u5957 Either \u7684\u60c5\u51b5\u5f88\u5e38\u89c1\u3002\u53ef\u4ee5\u4f7f\u7528 <code>flatMap<\/code> \u65b9\u6cd5\u5e73\u5c55\u5d4c\u5957\u7684 Either\uff1a<\/p>\n<pre>Either&lt;IOException, Integer&gt; result = readFileEither(filePath)\n  .flatMap(content -&gt; computeLengthEither(content));<\/pre>\n<p><strong>\u4f18\u70b9<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u4e2d\u7684 Either \u7c7b\u578b\u5177\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n<ul>\n<li>\u4ee3\u7801\u66f4\u6613\u8bfb\u548c\u7ef4\u62a4<\/li>\n<li>\u907f\u514d\u4e86\u4f20\u7edf\u7684\u5f02\u5e38\u5904\u7406<\/li>\n<li>\u53ef\u4ee5\u8f7b\u677e\u5730\u7ec4\u5408\u548c\u5904\u7406\u5d4c\u5957\u9519\u8bef<\/li>\n<\/ul>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aJava\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u9519\u8bef\u5904\u7406\u673a\u5236&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202817","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202817","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202817"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202817\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202817"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202817"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202817"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}