{"id":38885,"date":"2024-11-26T13:38:58","date_gmt":"2024-11-26T05:38:58","guid":{"rendered":"https:\/\/fwq.ai\/blog\/38885\/"},"modified":"2024-11-26T13:38:58","modified_gmt":"2024-11-26T05:38:58","slug":"%e9%a1%b9%e7%9b%ae-%e9%81%bf%e5%85%8d%e4%b8%8d%e5%bf%85%e8%a6%81%e5%9c%b0%e4%bd%bf%e7%94%a8%e6%a3%80%e6%9f%a5%e5%bc%82%e5%b8%b8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/38885\/","title":{"rendered":"\u9879\u76ee \u907f\u514d\u4e0d\u5fc5\u8981\u5730\u4f7f\u7528\u68c0\u67e5\u5f02\u5e38"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173164682183484.jpg\" class=\"aligncenter\" title=\"\u9879\u76ee \u907f\u514d\u4e0d\u5fc5\u8981\u5730\u4f7f\u7528\u68c0\u67e5\u5f02\u5e38\u63d2\u56fe\" alt=\"\u9879\u76ee \u907f\u514d\u4e0d\u5fc5\u8981\u5730\u4f7f\u7528\u68c0\u67e5\u5f02\u5e38\u63d2\u56fe\" \/><\/p>\n<p>\u68c0\u67e5\u5f02\u5e38\u662f java \u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u4eec\u8feb\u4f7f\u7a0b\u5e8f\u5458\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u53ef\u9760\u6027\u3002\u7136\u800c\uff0c\u8fc7\u5ea6\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4 api \u96be\u4ee5\u4f7f\u7528\u3002\u4e3a\u4e86\u8bc1\u660e\u68c0\u67e5\u5f02\u5e38\u662f\u5408\u7406\u7684\uff0c\u60c5\u51b5\u5fc5\u987b\u662f\u771f\u6b63\u7684\u5f02\u5e38\u5e76\u4e14\u7a0b\u5e8f\u5458\u80fd\u591f\u91c7\u53d6\u6709\u7528\u7684\u64cd\u4f5c\u3002\u5426\u5219\uff0c\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\u53ef\u80fd\u66f4\u5408\u9002\u3002<\/p>\n<p>java 8 \u7ed9\u68c0\u67e5\u5f02\u5e38\u7684\u4f7f\u7528\u5e26\u6765\u4e86\u989d\u5916\u7684\u6311\u6218\uff0c\u56e0\u4e3a\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u4e0d\u80fd\u76f4\u63a5\u5728\u6d41\u4e2d\u4f7f\u7528\uff0c\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u590d\u6742\u6027\uff0c\u53ef\u4ee5\u8003\u8651\u66ff\u4ee3\u65b9\u6848\uff0c\u4f8b\u5982\u8fd4\u56de\u4e00\u4e2a\u53ef\u9009\u5bf9\u8c61\u800c\u4e0d\u662f\u629b\u51fa\u53ef\u9a8c\u8bc1\u7684\u5f02\u5e38\uff0c\u6216\u8005\u5c06\u65b9\u6cd5\u5206\u4e3a\u4e24\u4e2a\uff0c\u4e00\u4e2a\u68c0\u67e5\u9519\u8bef\u7684\u53ef\u80fd\u6027\uff0c\u53e6\u4e00\u4e2a\u6267\u884c\u64cd\u4f5c\u3002<\/p>\n<p>\u5e94\u8c28\u614e\u4f7f\u7528\u53d7\u68c0\u5f02\u5e38\uff1a\u5982\u679c\u65e0\u6cd5\u6062\u590d\uff0c\u8bf7\u4f7f\u7528\u975e\u53d7\u68c0\u5f02\u5e38\u3002\u5f53\u6062\u590d\u53ef\u884c\u65f6\uff0c\u8003\u8651\u53ef\u9009\u8fd4\u56de\u3002\u5982\u679c\u8fd9\u6ca1\u6709\u63d0\u4f9b\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u90a3\u4e48\u68c0\u67e5\u5f02\u5e38\u53ef\u80fd\u662f\u5408\u7406\u7684\u3002<\/p>\n<p>\u52a0\uff1a<br \/> \u8fd9\u91cc\u6709\u4e00\u4e9b\u4f8b\u5b50\u6765\u8bf4\u660e\u68c0\u67e5\u5f02\u5e38\u548c\u66ff\u4ee3\u65b9\u6848\uff08\u4f8b\u5982optional\uff09\u7684\u6b63\u786e\u4f7f\u7528\u4ee5\u53ca\u68c0\u67e5\u65b9\u6cd5\u7684\u4f7f\u7528\u3002<\/p>\n<p><strong>\u8c28\u614e\u4f7f\u7528\u68c0\u67e5\u5f02\u5e38<\/strong><br \/> \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4ece\u6587\u4ef6\u52a0\u8f7d\u6570\u636e\u7684\u65b9\u6cd5\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u60f3\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u6765\u901a\u77e5\u8c03\u7528\u8005\u6709\u95ee\u9898\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u68c0\u67e5\u5f02\u5e38\u662f\u5408\u9002\u7684\uff0c\u56e0\u4e3a\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u91c7\u53d6\u63aa\u65bd\u89e3\u51b3\u95ee\u9898\uff08\u4f8b\u5982\u63d0\u4f9b\u6b63\u786e\u7684\u5b58\u6863\u8def\u5f84\uff09\u3002<\/p>\n<pre>public class fileloader {\n    public string loadfile(string filepath) throws filenotfoundexception {\n        file file = new file(filepath);\n        if (!file.exists()) {\n            throw new filenotfoundexception(\"arquivo n\u00e3o encontrado: \" + filepath);\n        }\n        \/\/ c\u00f3digo para carregar o arquivo\n        return \"conte\u00fado do arquivo\";\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u8c03\u7528\u8005\u9700\u8981\u5904\u7406 filenotfoundexception \u5f02\u5e38\uff0c\u56e0\u4e3a\u4ed6\u53ef\u4ee5\u901a\u8fc7\u63d0\u4f9b\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\u6765\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n<p><strong>\u4f7f\u7528\u53ef\u9009\u5f02\u5e38\u4ee3\u66ff\u68c0\u67e5\u5f02\u5e38<\/strong><br \/> \u5982\u679c\u4e22\u5931\u6587\u4ef6\u662f\u4e0d\u5e94\u8be5\u4e2d\u65ad\u6b63\u5e38\u6267\u884c\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528optional\u6765\u6307\u793a\u7ed3\u679c\u53ef\u4ee5\u4e22\u5931\u800c\u4e0d\u629b\u51fa\u5f02\u5e38\u3002\u8fd9\u4f7f\u5f97 api \u5728\u53ef\u4ee5\u5bb9\u5fcd\u7f3a\u4e4f\u4ef7\u503c\u7684\u60c5\u51b5\u4e0b\u66f4\u52a0\u6d41\u7545\u3002<\/p>\n<pre>import java.util.optional;\n\npublic class fileloader {\n    public optional&lt;string&gt; loadfile(string filepath) {\n        file file = new file(filepath);\n        if (!file.exists()) {\n            return optional.empty();\n        }\n        \/\/ c\u00f3digo para carregar o arquivo\n        return optional.of(\"conte\u00fado do arquivo\");\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8c03\u7528\u8005\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>fileloader loader = new fileloader();\noptional&lt;string&gt; content = loader.loadfile(\"caminho\/para\/arquivo.txt\");\ncontent.ifpresentorelse(\n    system.out::println,\n    () -&gt; system.out.println(\"arquivo n\u00e3o encontrado.\")\n);\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u8c03\u7528\u8005\u4e0d\u9700\u8981\u5904\u7406\u5f02\u5e38\uff0c\u800coptional\u53ef\u4ee5\u8ba9\u5b83\u66f4\u987a\u5229\u5730\u5904\u7406\u7f3a\u5931\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u65b9\u6cd5\u5206\u4e3a\u4e24\u79cd\uff1a\u9a8c\u8bc1\u548c\u64cd\u4f5c<\/strong><br \/> \u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8be5\u65b9\u6cd5\u5206\u4e3a\u4e24\u79cd\uff1a\u4e00\u79cd\u68c0\u67e5\u6761\u4ef6\uff0c\u53e6\u4e00\u79cd\u6267\u884c\u64cd\u4f5c\u3002\u8fd9\u5141\u8bb8\u8c03\u7528\u8005\u5728\u8c03\u7528 mn \u65b9\u6cd5\u4e4b\u524d\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u4f7f api \u66f4\u52a0\u7075\u6d3b\u3002<\/p>\n<pre>public class fileloader {\n    \/\/ verifica se o arquivo est\u00e1 dispon\u00edvel\n    public boolean isfileavailable(string filepath) {\n        file file = new file(filepath);\n        return file.exists();\n    }\n\n    \/\/ carrega o conte\u00fado do arquivo\n    public string loadfile(string filepath) {\n        if (!isfileavailable(filepath)) {\n            throw new illegalstateexception(\"arquivo n\u00e3o encontrado: \" + filepath);\n        }\n        \/\/ c\u00f3digo para carregar o arquivo\n        return \"conte\u00fado do arquivo\";\n    }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8c03\u7528\u8005\u4ee3\u7801\u4e2d\uff1a<\/p>\n<pre>FileLoader loader = new FileLoader();\nString filePath = \"caminho\/para\/arquivo.txt\";\n\nif (loader.isFileAvailable(filePath)) {\n    String content = loader.loadFile(filePath);\n    System.out.println(content);\n} else {\n    System.out.println(\"Arquivo n\u00e3o encontrado.\");\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528 illegalstateexception\uff0c\u8fd9\u662f\u4e00\u79cd\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\uff0c\u5982\u679c\u8c03\u7528\u8005\u5c1d\u8bd5\u52a0\u8f7d\u6587\u4ef6\u800c\u4e0d\u9996\u5148\u68c0\u67e5\u5b83\u662f\u5426\u53ef\u7528\u3002<\/p>\n<p><strong>\u603b\u7ed3<\/strong><br \/> checked exception\uff1a\u7528\u4e8e\u8c03\u7528\u8005\u53ef\u4ee5\u76f4\u63a5\u4fee\u590d\u7684\u6761\u4ef6\uff0c\u4f8b\u5982\u6587\u4ef6\u7684 filenotfoundexception\u3002<br \/> \u53ef\u9009\uff1a\u5f53\u7f3a\u5c11\u503c\u4e0d\u5e94\u4e2d\u65ad\u7a0b\u5e8f\u6d41\u7a0b\u65f6\u4f7f\u7528\u3002<\/p>\n<p>\u65b9\u6cd5\u5212\u5206\uff1a\u5728\u53ef\u4ee5\u9884\u6d4b\u548c\u907f\u514d\u9519\u8bef\u7684\u60c5\u51b5\u4e0b\uff0c\u5c06\u68c0\u67e5\u548c\u6267\u884c\u65b9\u6cd5\u5206\u5f00\uff0c\u4f7fapi\u66f4\u52a0\u7075\u6d3b\u548c\u7b80\u5355\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u9879\u76ee \u907f\u514d\u4e0d\u5fc5\u8981\u5730\u4f7f\u7528\u68c0\u67e5\u5f02\u5e38\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u68c0\u67e5\u5f02\u5e38\u662f java \u4e2d\u7684\u4e00\u4e2a\u5f3a\u5927\u5de5\u5177\uff0c\u56e0\u4e3a\u5b83\u4eec\u8feb\u4f7f\u7a0b\u5e8f\u5458\u5904\u7406\u5f02\u5e38\u60c5\u51b5\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u53ef\u9760\u6027\u3002\u7136\u800c\uff0c\u8fc7\u5ea6\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4 api \u96be\u4ee5\u4f7f\u7528\u3002\u4e3a\u4e86\u8bc1\u660e\u68c0\u67e5\u5f02\u5e38\u662f\u5408\u7406\u7684\uff0c\u60c5\u51b5\u5fc5\u987b\u662f\u771f\u6b63\u7684\u5f02\u5e38\u5e76\u4e14\u7a0b\u5e8f\u5458\u80fd\u591f\u91c7\u53d6\u6709\u7528\u7684\u64cd\u4f5c\u3002\u5426\u5219\uff0c\u672a\u7ecf\u68c0\u67e5\u7684\u5f02\u5e38\u53ef\u80fd\u66f4\u5408\u9002\u3002 java 8 \u7ed9\u68c0\u67e5\u5f02\u5e38\u7684\u4f7f\u7528\u5e26\u6765\u4e86\u989d\u5916\u7684\u6311\u6218\uff0c\u56e0\u4e3a\u629b\u51fa\u5f02\u5e38\u7684\u65b9\u6cd5\u4e0d\u80fd\u76f4\u63a5\u5728\u6d41\u4e2d\u4f7f\u7528\uff0c\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u3002\u4e3a\u4e86\u907f\u514d\u8fd9\u79cd\u590d\u6742\u6027\uff0c\u53ef\u4ee5\u8003\u8651\u66ff\u4ee3\u65b9\u6848\uff0c\u4f8b\u5982\u8fd4\u56de\u4e00\u4e2a\u53ef\u9009\u5bf9\u8c61\u800c\u4e0d\u662f\u629b\u51fa\u53ef\u9a8c\u8bc1\u7684\u5f02\u5e38\uff0c\u6216\u8005\u5c06\u65b9\u6cd5\u5206\u4e3a\u4e24\u4e2a\uff0c\u4e00\u4e2a\u68c0\u67e5\u9519\u8bef\u7684\u53ef\u80fd\u6027\uff0c\u53e6\u4e00\u4e2a\u6267\u884c\u64cd\u4f5c\u3002 \u5e94\u8c28\u614e\u4f7f\u7528\u53d7\u68c0\u5f02\u5e38\uff1a\u5982\u679c\u65e0\u6cd5\u6062\u590d\uff0c\u8bf7\u4f7f\u7528\u975e\u53d7\u68c0\u5f02\u5e38\u3002\u5f53\u6062\u590d\u53ef\u884c\u65f6\uff0c\u8003\u8651\u53ef\u9009\u8fd4\u56de\u3002\u5982\u679c\u8fd9\u6ca1\u6709\u63d0\u4f9b\u8db3\u591f\u7684\u4fe1\u606f\uff0c\u90a3\u4e48\u68c0\u67e5\u5f02\u5e38\u53ef\u80fd\u662f\u5408\u7406\u7684\u3002 \u52a0\uff1a \u8fd9\u91cc\u6709\u4e00\u4e9b\u4f8b\u5b50\u6765\u8bf4\u660e\u68c0\u67e5\u5f02\u5e38\u548c\u66ff\u4ee3\u65b9\u6848\uff08\u4f8b\u5982optional\uff09\u7684\u6b63\u786e\u4f7f\u7528\u4ee5\u53ca\u68c0\u67e5\u65b9\u6cd5\u7684\u4f7f\u7528\u3002 \u8c28\u614e\u4f7f\u7528\u68c0\u67e5\u5f02\u5e38 \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4ece\u6587\u4ef6\u52a0\u8f7d\u6570\u636e\u7684\u65b9\u6cd5\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u60f3\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u6765\u901a\u77e5\u8c03\u7528\u8005\u6709\u95ee\u9898\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u68c0\u67e5\u5f02\u5e38\u662f\u5408\u9002\u7684\uff0c\u56e0\u4e3a\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u91c7\u53d6\u63aa\u65bd\u89e3\u51b3\u95ee\u9898\uff08\u4f8b\u5982\u63d0\u4f9b\u6b63\u786e\u7684\u5b58\u6863\u8def\u5f84\uff09\u3002 public class fileloader { public string loadfile(string filepath) throws filenotfoundexception { file file = new file(filepath); if (!file.exists()) { throw new filenotfoundexception(&#8220;arquivo n\u00e3o encontrado: &#8221; + filepath); } \/\/ c\u00f3digo para carregar o arquivo return &#8220;conte\u00fado do arquivo&#8221;; } } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u8c03\u7528\u8005\u9700\u8981\u5904\u7406 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-38885","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=38885"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/38885\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=38885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=38885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=38885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}