{"id":1321,"date":"2022-10-30T22:44:26","date_gmt":"2022-10-30T14:44:26","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1321"},"modified":"2022-11-27T12:39:46","modified_gmt":"2022-11-27T04:39:46","slug":"io%e6%b5%81-%e8%bd%ac%e6%8d%a2%e6%b5%81%e6%a6%82%e8%bf%b0%e4%b8%8einputstreamreader%e7%9a%84%e4%bd%bf%e7%94%a8%e3%80%81%e8%bd%ac%e6%8d%a2%e6%b5%81%e5%ae%9e%e7%8e%b0%e6%96%87%e4%bb%b6%e7%9a%84%e8%af%bb","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1321","title":{"rendered":"IO\u6d41-\u8f6c\u6362\u6d41\u6982\u8ff0\u4e0eInputStreamReader\u7684\u4f7f\u7528\u3001\u8f6c\u6362\u6d41\u5b9e\u73b0\u6587\u4ef6\u7684\u8bfb\u5165\u548c\u5199\u51fa\u3001\u591a\u79cd\u5b57\u7b26\u7f16\u7801\u96c6\u7684\u8bf4\u660e"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P600;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.io.*;\r\n\r\n\/**\r\n * \u5904\u7406\u6d41\u4e4b\u4e8c\uff1a\u8f6c\u6362\u6d41\u7684\u4f7f\u7528\r\n * 1. \u8f6c\u6362\u6d41\uff1a\u5c5e\u4e8e\u5b57\u7b26\u6d41\r\n *      InputStreamReader\uff1a\u5c06\u4e00\u4e2a\u5b57\u8282\u7684\u8f93\u5165\u6d41\u8f6c\u6362\u4e3a\u5b57\u7b26\u7684\u8f93\u5165\u6d41\r\n *      OutputStreamWriter\uff1a\u5c06\u4e00\u4e2a\u5b57\u7b26\u7684\u8f93\u51fa\u6d41\u8f6c\u6362\u4e3a\u5b57\u8282\u7684\u8f93\u51fa\u6d41\r\n\r\n * 2. \u4f5c\u7528\uff1a\u63d0\u4f9b\u5b57\u8282\u6d41\u4e0e\u5b57\u7b26\u6d41\u4e4b\u95f4\u7684\u8f6c\u6362\r\n *\r\n * 3. \u89e3\u7801\uff1a\u5b57\u8282\u3001\u5b57\u8282\u6570\u7ec4 ---> \u5b57\u7b26\u6570\u7ec4\u3001\u5b57\u7b26\u4e32\r\n *    \u7f16\u7801\uff1b\u5b57\u7b26\u6570\u7ec4\u3001\u5b57\u7b26\u4e32 ---> \u5b57\u8282\u3001\u5b57\u8282\u6570\u7ec4\r\n *\r\n * 4. \u5b57\u7b26\u96c6\r\n * \uf0d8 ASCII\uff1a\u7f8e\u56fd\u6807\u51c6\u4fe1\u606f\u4ea4\u6362\u7801\u3002\r\n * \uf0fc \u7528\u4e00\u4e2a\u5b57\u8282\u76847\u4f4d\u53ef\u4ee5\u8868\u793a\u3002\r\n * \uf0d8 ISO8859-1\uff1a\u62c9\u4e01\u7801\u8868\u3002\u6b27\u6d32\u7801\u8868\r\n * \uf0fc \u7528\u4e00\u4e2a\u5b57\u8282\u76848\u4f4d\u8868\u793a\u3002\r\n * \uf0d8 GB2312\uff1a\u4e2d\u56fd\u7684\u4e2d\u6587\u7f16\u7801\u8868\u3002\u6700\u591a\u4e24\u4e2a\u5b57\u8282\u7f16\u7801\u6240\u6709\u5b57\u7b26\r\n * \uf0d8 GBK\uff1a\u4e2d\u56fd\u7684\u4e2d\u6587\u7f16\u7801\u8868\u5347\u7ea7\uff0c\u878d\u5408\u4e86\u66f4\u591a\u7684\u4e2d\u6587\u6587\u5b57\u7b26\u53f7\u3002\u6700\u591a\u4e24\u4e2a\u5b57\u8282\u7f16\u7801\r\n * \uf0d8 Unicode\uff1a\u56fd\u9645\u6807\u51c6\u7801\uff0c\u878d\u5408\u4e86\u76ee\u524d\u4eba\u7c7b\u4f7f\u7528\u7684\u6240\u6709\u5b57\u7b26\u3002\u4e3a\u6bcf\u4e2a\u5b57\u7b26\u5206\u914d\u552f\u4e00\u7684\u5b57\u7b26\u7801\u3002\u6240\u6709\u7684\u6587\u5b57\u90fd\u7528\u4e24\u4e2a\u5b57\u8282\u6765\u8868\u793a\u3002\r\n * \uf0d8 UTF-8\uff1a\u53d8\u957f\u7684\u7f16\u7801\u65b9\u5f0f\uff0c\u53ef\u75281-4\u4e2a\u5b57\u8282\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\u3002\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-30 \u4e0b\u5348 12:47\r\n *\/\r\npublic class InputStreamReaderTest {\r\n\r\n    \/*\r\n    \u6b64\u65f6\u5904\u7406\u5f02\u5e38\u7684\u8bdd\uff0c\u4ecd\u7136\u4f7f\u7528try-catch-finally\r\n    InputStreamReader\u7684\u4f7f\u7528\uff0c\u5b9e\u73b0\u5b57\u8282\u7684\u8f93\u5165\u6d41\u5230\u5b57\u7b26\u7684\u8f93\u5165\u6d41\u7684\u8f6c\u6362\r\n     *\/\r\n    @Test\r\n    public void test1() throws IOException {\r\n        FileInputStream fis = new FileInputStream(\"dbcp.txt\");\r\n        InputStreamReader isr = new InputStreamReader(fis);\/\/\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7684\u5b57\u7b26\u96c6\r\n        \/\/\u53c2\u65702\u6307\u660e\u4e86\u5b57\u7b26\u96c6\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u4e2a\u5b57\u7b26\u96c6\uff0c\u53d6\u51b3\u4e8e\u6587\u4ef6dbcp.txt\u4fdd\u5b58\u65f6\u4f7f\u7528\u7684\u5b57\u7b26\u96c6\r\n\/\/        InputStreamReader isr = new InputStreamReader(fis,\"UTF-8\");\/\/\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7684\u5b57\u7b26\u96c6\r\n\r\n        char&#91;] cbuf = new char&#91;20];\r\n        int len;\r\n        while ((len = isr.read(cbuf)) != -1){\r\n            String str = new String(cbuf,0,len);\r\n            System.out.print(str);\r\n        }\r\n\r\n        isr.close();\r\n\r\n    }\r\n\r\n    \/*\r\n    \u6b64\u65f6\u5904\u7406\u5f02\u5e38\u7684\u8bdd\uff0c\u4ecd\u7136\u4f7f\u7528try-catch-finally\r\n\r\n    \u7efc\u5408\u4f7f\u7528InputStreamReader\u548cOutPutStreamWriter\r\n     *\/\r\n    @Test\r\n    public void test2() throws Exception {\r\n\r\n        \/\/1. \u9020\u6587\u4ef6\u3001\u9020\u6d41\r\n        File file1 = new File(\"dbcp.txt\");\r\n        File file2 = new File(\"dbcp_gbk.txt\");\r\n\r\n        FileInputStream fis = new FileInputStream(file1);\r\n        FileOutputStream fos = new FileOutputStream(file2);\r\n\r\n        InputStreamReader isr = new InputStreamReader(fis,\"utf-8\");\r\n        OutputStreamWriter osw = new OutputStreamWriter(fos,\"gbk\");\r\n\r\n        \/\/2. \u8bfb\u5199\u8fc7\u7a0b\r\n        char&#91;] cbuf = new char&#91;20];\r\n        int len;\r\n        while ((len = isr.read(cbuf)) != -1){\r\n            osw.write(cbuf,0,len);\r\n        }\r\n\r\n        \/\/3. \u5173\u95ed\u8d44\u6e90\r\n        isr.close();\r\n        osw.close();\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[46],"class_list":["post-1321","post","type-post","status-publish","format-standard","hentry","category-java-code","tag-java"],"_links":{"self":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1321"}],"collection":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1321"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1321\/revisions"}],"predecessor-version":[{"id":1322,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1321\/revisions\/1322"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}