{"id":36516,"date":"2024-11-26T17:18:09","date_gmt":"2024-11-26T09:18:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36516\/"},"modified":"2024-11-26T17:18:09","modified_gmt":"2024-11-26T09:18:09","slug":"%e4%bd%bf%e7%94%a8java%e8%af%ad%e8%a8%80%e5%9f%ba%e4%ba%8e%e5%ad%97%e7%ac%a6%e7%9a%84%e6%b5%81","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36516\/","title":{"rendered":"\u4f7f\u7528Java\u8bed\u8a00\u57fa\u4e8e\u5b57\u7b26\u7684\u6d41"},"content":{"rendered":"<p>\u4e3a\u4e86\u6267\u884c\u57fa\u4e8e\u5b57\u7b26\u7684 i\/o \u64cd\u4f5c\uff0cjava \u63d0\u4f9b\u4e86\u81ea\u5df1\u7684\u57fa\u4e8e\u5b57\u7b26\u7684\u6d41\u5c42\u6b21\u7ed3\u6784\uff0c\u4ee5\u53ca reader \u548c writer \u7b49\u62bd\u8c61\u7c7b\u3002\u8fd9\u4e9b\u7c7b\u5141\u8bb8\u60a8\u76f4\u63a5\u8bfb\u5199\u5b57\u7b26\uff0c\u4f7f\u5b83\u4eec\u6bd4\u5b57\u8282\u6d41\u66f4\u9002\u5408\u6587\u672c\u6570\u636e\u3002\u8fd9\u4e9b\u7c7b\u7684\u4e3b\u8981\u65b9\u6cd5\u5904\u7406\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\uff0c\u5e76\u5728\u53d1\u751f\u9519\u8bef\u65f6\u629b\u51fa ioexception\u3002<\/p>\n<p><strong>\u5b57\u7b26\u6d41\u7ed3\u6784<\/strong><\/p>\n<p><strong>\u4e3b\u8981\u62bd\u8c61\u7c7b\uff1a<\/strong><\/p>\n<ul>\n<li>reader\uff1a\u8bfb\u53d6\u5b57\u7b26\u7684\u57fa\u7840\u3002<\/li>\n<li>writer\uff1a\u4e66\u5199\u89d2\u8272\u7684\u57fa\u7840\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u7c7b\u6784\u6210\u4e86\u5b57\u7b26\u6d41 i\/o \u64cd\u4f5c\u7684\u6700\u5c0f\u7ed3\u6784\uff0c\u5176\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u5b50\u7c7b\u3002<\/p>\n<p><strong>\u5e26\u6709\u5b57\u7b26\u6d41\u7684\u63a7\u5236\u53f0\u8f93\u5165<\/strong><br \/> \u5bf9\u4e8e\u56fd\u9645\u5316\u7a0b\u5e8f\u6216\u64cd\u4f5c\u6587\u672c\u7684\u7a0b\u5e8f\uff0c\u6700\u597d\u4f7f\u7528\u5b57\u7b26\u6d41\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u5b57\u7b26\u3002\u7531\u4e8esystem.in\u662f\u5b57\u8282\u6d41\uff0c\u56e0\u6b64\u9700\u8981\u9488\u5bf9\u5b57\u7b26\u6d41\u8fdb\u884c\u9002\u914d\u3002 <\/p>\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>inputstreamreader\uff1a\u5c06\u5b57\u8282\u8f6c\u6362\u4e3a\u5b57\u7b26\u3002<\/li>\n<li>bufferedreader\uff1a\u901a\u8fc7\u7f13\u51b2\u8f93\u5165\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/li>\n<\/ul>\n<p><strong>\u8bfb\u53d6\u63a7\u5236\u53f0\u8f93\u5165\u7684\u793a\u4f8b<\/strong><br \/> \u8981\u4f7f\u7528 bufferedreader \u8bfb\u53d6\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 inputstreamreader \u5c06 system.in \u8f6c\u6362\u4e3a\u5b57\u7b26\u6d41\uff1a<\/p>\n<pre>import java.io.*;\n\npublic class ConsoleReaderExample {\n  public static void main(String args[]) {\n    try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {\n      System.out.print(\"Digite uma linha de texto: \");\n      String linha = reader.readLine();\n      System.out.println(\"Voc\u00ea digitou: \" + linha);\n    } catch(IOException e) {\n      System.out.println(\"Erro de entrada\/sa\u00edda: \" + e);\n    }\n  }\n}\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4ee3\u7801\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li>inputstreamreader\uff1a\u5c06 system.in \u4e2d\u7684\u5b57\u8282\u6d41\u8f6c\u6362\u4e3a\u5b57\u7b26\u6d41\u3002<\/li>\n<li>bufferedreader\uff1a\u7528\u4e8e\u9ad8\u6548\u8bfb\u53d6\u5b57\u7b26\u3001\u5b57\u7b26\u4e32\u548c\u884c\u3002<\/li>\n<\/ul>\n<p><strong>\u89d2\u8272\u6d41\u7684\u4f18\u70b9<\/strong><\/p>\n<ul>\n<li>\u56fd\u9645\u5316\uff1a\u517c\u5bb9\u4e0d\u540c\u7684\u5b57\u7b26\u96c6\u3002<\/li>\n<li>\u6548\u7387\uff1abufferedreader \u5141\u8bb8\u60a8\u4e00\u6b21\u8bfb\u53d6\u6574\u884c\uff0c\u7b80\u5316\u6d41\u7a0b\u5e76\u51cf\u5c11\u7cfb\u7edf\u8c03\u7528\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u4e9b\u5b57\u7b26\u6d41\u4f7f\u6587\u672c\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u5bb9\u6613\uff0c\u975e\u5e38\u9002\u5408\u4ee5\u5b57\u7b26\u548c\u6587\u672c\u4e3a\u4e3b\u8981\u7126\u70b9\u7684\u6570\u636e\u8f93\u5165\u548c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173128447894312.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528Java\u8bed\u8a00\u57fa\u4e8e\u5b57\u7b26\u7684\u6d41\u63d2\u56fe\" alt=\"\u4f7f\u7528Java\u8bed\u8a00\u57fa\u4e8e\u5b57\u7b26\u7684\u6d41\u63d2\u56fe\" \/><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Java\u8bed\u8a00\u57fa\u4e8e\u5b57\u7b26\u7684\u6d41\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>\u4e3a\u4e86\u6267\u884c\u57fa\u4e8e\u5b57\u7b26\u7684 i\/o \u64cd\u4f5c\uff0cjava \u63d0\u4f9b\u4e86\u81ea\u5df1\u7684\u57fa\u4e8e\u5b57\u7b26\u7684\u6d41\u5c42\u6b21\u7ed3\u6784\uff0c\u4ee5\u53ca reader \u548c writer \u7b49\u62bd\u8c61\u7c7b\u3002\u8fd9\u4e9b\u7c7b\u5141\u8bb8\u60a8\u76f4\u63a5\u8bfb\u5199\u5b57\u7b26\uff0c\u4f7f\u5b83\u4eec\u6bd4\u5b57\u8282\u6d41\u66f4\u9002\u5408\u6587\u672c\u6570\u636e\u3002\u8fd9\u4e9b\u7c7b\u7684\u4e3b\u8981\u65b9\u6cd5\u5904\u7406\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\uff0c\u5e76\u5728\u53d1\u751f\u9519\u8bef\u65f6\u629b\u51fa ioexception\u3002 \u5b57\u7b26\u6d41\u7ed3\u6784 \u4e3b\u8981\u62bd\u8c61\u7c7b\uff1a reader\uff1a\u8bfb\u53d6\u5b57\u7b26\u7684\u57fa\u7840\u3002 writer\uff1a\u4e66\u5199\u89d2\u8272\u7684\u57fa\u7840\u3002 \u8fd9\u4e9b\u7c7b\u6784\u6210\u4e86\u5b57\u7b26\u6d41 i\/o \u64cd\u4f5c\u7684\u6700\u5c0f\u7ed3\u6784\uff0c\u5176\u65b9\u6cd5\u9002\u7528\u4e8e\u6240\u6709\u5b50\u7c7b\u3002 \u5e26\u6709\u5b57\u7b26\u6d41\u7684\u63a7\u5236\u53f0\u8f93\u5165 \u5bf9\u4e8e\u56fd\u9645\u5316\u7a0b\u5e8f\u6216\u64cd\u4f5c\u6587\u672c\u7684\u7a0b\u5e8f\uff0c\u6700\u597d\u4f7f\u7528\u5b57\u7b26\u6d41\u4ece\u63a7\u5236\u53f0\u8bfb\u53d6\u5b57\u7b26\u3002\u7531\u4e8esystem.in\u662f\u5b57\u8282\u6d41\uff0c\u56e0\u6b64\u9700\u8981\u9488\u5bf9\u5b57\u7b26\u6d41\u8fdb\u884c\u9002\u914d\u3002 \u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b inputstreamreader\uff1a\u5c06\u5b57\u8282\u8f6c\u6362\u4e3a\u5b57\u7b26\u3002 bufferedreader\uff1a\u901a\u8fc7\u7f13\u51b2\u8f93\u5165\u6765\u63d0\u9ad8\u6548\u7387\u3002 \u8bfb\u53d6\u63a7\u5236\u53f0\u8f93\u5165\u7684\u793a\u4f8b \u8981\u4f7f\u7528 bufferedreader \u8bfb\u53d6\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528 inputstreamreader \u5c06 system.in \u8f6c\u6362\u4e3a\u5b57\u7b26\u6d41\uff1a import java.io.*; public class ConsoleReaderExample { public static void main(String args[]) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) { System.out.print(&#8220;Digite uma linha de texto: [&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-36516","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36516","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=36516"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36516\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}