{"id":1323,"date":"2022-10-30T22:45:00","date_gmt":"2022-10-30T14:45:00","guid":{"rendered":"https:\/\/qaqaq.top\/?p=1323"},"modified":"2022-11-27T12:39:46","modified_gmt":"2022-11-27T04:39:46","slug":"io%e6%b5%81-%e6%a0%87%e5%87%86%e7%9a%84%e8%be%93%e5%85%a5%e3%80%81%e8%be%93%e5%87%ba%e6%b5%81%e3%80%81%e6%89%93%e5%8d%b0%e6%b5%81%e7%9a%84%e4%bd%bf%e7%94%a8%e3%80%81%e6%95%b0%e6%8d%ae%e6%b5%81","status":"publish","type":"post","link":"https:\/\/qaqaq.top\/?p=1323","title":{"rendered":"IO\u6d41-\u6807\u51c6\u7684\u8f93\u5165\u3001\u8f93\u51fa\u6d41\u3001\u6253\u5370\u6d41\u7684\u4f7f\u7528\u3001\u6570\u636e\u6d41\u7684\u4f7f\u7528"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P603;\r\n\r\nimport org.junit.jupiter.api.Test;\r\n\r\nimport java.io.*;\r\n\r\n\/**\r\n * \u5176\u4ed6\u6d41\u7684\u4f7f\u7528\r\n * 1. \u6807\u51c6\u7684\u8f93\u5165\u3001\u8f93\u51fa\u6d41\r\n * 2. \u6253\u5370\u6d41\r\n * 3. \u6570\u636e\u6d41\r\n *\r\n * @author RichieZhang\r\n * @create 2022-10-30 \u4e0b\u5348 2:49\r\n *\/\r\npublic class OtherStreamTest {\r\n\r\n    \/*\r\n    1. \u6807\u51c6\u7684\u8f93\u5165\u3001\u8f93\u51fa\u6d41\r\n    1.1\r\n    System.in:\u6807\u51c6\u7684\u8f93\u5165\u6d41\uff0c\u9ed8\u8ba4\u4ece\u952e\u76d8\u8f93\u5165\r\n    System.out:\u6807\u51c6\u7684\u8f93\u51fa\u6d41\uff0c\u9ed8\u8ba4\u4ece\u63a7\u5236\u53f0\u8f93\u51fa\r\n    1.2\r\n    System\u7c7b\u7684setIn(InputStream is) \/ setOut(PrintStream ps) \u65b9\u5f0f\u91cd\u65b0\u6307\u5b9a\u8f93\u5165\u548c\u8f93\u51fa\u7684\u6d41\u3002\r\n\r\n    1.3\u7ec3\u4e60\uff1a\r\n    \u4ece\u952e\u76d8\u8f93\u5165\u5b57\u7b26\u4e32\uff0c\u8981\u6c42\u5c06\u8bfb\u53d6\u5230\u7684\u6574\u884c\u5b57\u7b26\u4e32\u8f6c\u6210\u5927\u5199\u8f93\u51fa\u3002\u7136\u540e\u7ee7\u7eed\u8fdb\u884c\u8f93\u5165\u64cd\u4f5c\uff0c\r\n    \u76f4\u81f3\u5f53\u8f93\u5165\u201ce\u201d\u6216\u8005\u201cexit\u201d\u65f6\uff0c\u9000\u51fa\u7a0b\u5e8f\u3002\r\n\r\n    \u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528Scanner\u5b9e\u73b0\uff0c\u8c03\u7528next()\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\r\n    \u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528System.in\u5b9e\u73b0\u3002System.in ---> \u8f6c\u6362\u6d41 ---> BufferedReader\u7684readLine()\r\n\r\n     *\/\r\n\r\n    public static void main(String&#91;] args) {\r\n\r\n        BufferedReader br = null;\r\n        try {\r\n            InputStreamReader isr = new InputStreamReader(System.in);\r\n            br = new BufferedReader(isr);\r\n            while (true) {\r\n                System.out.println(\"\u8bf7\u8f93\u5165\u5b57\u7b26\u4e32\uff1a\");\r\n                String data = br.readLine();\r\n                if (\"e\".equalsIgnoreCase(data) || \"exit\".equalsIgnoreCase(data)) {\r\n                    System.out.println(\"\u7a0b\u5e8f\u7ed3\u675f\");\r\n                    break;\r\n                }\r\n\r\n                String upperCase = data.toUpperCase();\r\n                System.out.println(upperCase);\r\n\r\n            }\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (br != null) {\r\n\r\n                try {\r\n                    br.close();\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n            }\r\n        }\r\n\r\n    }\r\n\r\n    \/*\r\n    2. \u6253\u5370\u6d41\uff1aPrintStream \u548c PrintWriter\r\n\r\n    2.1 \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u91cd\u8f7d\u7684print() \u548c Println()\r\n    2.2 \u7ec3\u4e60\uff1a\r\n\r\n     *\/\r\n    @Test\r\n    public void test2() {\r\n        PrintStream ps = null;\r\n        try {\r\n            FileOutputStream fos = new FileOutputStream(new File(\"text.txt\"));\r\n            \/\/ \u521b\u5efa\u6253\u5370\u8f93\u51fa\u6d41,\u8bbe\u7f6e\u4e3a\u81ea\u52a8\u5237\u65b0\u6a21\u5f0f(\u5199\u5165\u6362\u884c\u7b26\u6216\u5b57\u8282 '\\n' \u65f6\u90fd\u4f1a\u5237\u65b0\u8f93\u51fa\u7f13\u51b2\u533a)\r\n            ps = new PrintStream(fos, true);\r\n            if (ps != null) {\/\/ \u628a\u6807\u51c6\u8f93\u51fa\u6d41(\u63a7\u5236\u53f0\u8f93\u51fa)\u6539\u6210\u6587\u4ef6\r\n                System.setOut(ps);\r\n            }\r\n\r\n            for (int i = 0; i &lt;= 255; i++) { \/\/ \u8f93\u51faASCII\u5b57\u7b26\r\n                System.out.print((char) i);\r\n                if (i % 50 == 0) { \/\/ \u6bcf50\u4e2a\u6570\u636e\u4e00\u884c\r\n                    System.out.println(); \/\/ \u6362\u884c\r\n                }\r\n            }\r\n\r\n        } catch (FileNotFoundException e) {\r\n            e.printStackTrace();\r\n        } finally {\r\n            if (ps != null) {\r\n                ps.close();\r\n            }\r\n        }\r\n    }\r\n\r\n    \/*\r\n    3. \u6570\u636e\u6d41\r\n    3.1 DataInputStream \u548c DataOutputStream\r\n    3.2 \u4f5c\u7528\uff1a\u7528\u4e8e\u8bfb\u53d6\u6216\u5199\u51fa\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u6216\u5b57\u7b26\u4e32\r\n\r\n    \u7ec3\u4e60\uff1a\u5c06\u5185\u5b58\u4e2d\u7684\u5b57\u7b26\u4e32\u3001\u57fa\u672c\u6570\u636e\u7c7b\u578b\u7684\u53d8\u91cf\u5199\u51fa\u5230\u6587\u4ef6\u4e2d\u3002\r\n\r\n    \u6ce8\u610f\uff1a\u5904\u7406\u5f02\u5e38\u7684\u8bdd\uff0c\u4ecd\u7136\u5e94\u8be5\u4f7f\u7528try-catch-finally.\r\n     *\/\r\n\r\n    @Test\r\n    public void test3() throws IOException {\r\n\r\n        \/\/1.\r\n        DataOutputStream dos = new DataOutputStream(new FileOutputStream(\"data.txt\"));\r\n        \/\/2.\r\n        dos.writeUTF(\"\u5f20\u4e09\");\r\n        dos.flush();\/\/\u5237\u65b0\u64cd\u4f5c\uff0c\u5c06\u5185\u5b58\u4e2d\u7684\u6570\u636e\u5199\u5165\u6587\u4ef6\r\n        dos.writeInt(23);\r\n        dos.flush();\r\n        dos.writeBoolean(true);\r\n        dos.flush();\r\n        \/\/3.\r\n        dos.close();\r\n    }\r\n\r\n    \/*\r\n    \u5c06\u6587\u4ef6\u4e2d\u5b58\u50a8\u7684\u57fa\u672c\u6570\u636e\u7c7b\u578b\u53d8\u91cf\u548c\u5b57\u7b26\u4e32\u8bfb\u53d6\u5230\u5185\u5b58\u4e2d\uff0c\u4fdd\u5b58\u5728\u53d8\u91cf\u4e2d\u3002\r\n\r\n    \u6ce8\u610f\u70b9\uff1a\u8bfb\u53d6\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u7684\u987a\u5e8f\u8981\u4e0e\u5f53\u521d\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u4fdd\u5b58\u7684\u6570\u636e\u7684\u987a\u5e8f\u4e00\u81f4\uff01\r\n     *\/\r\n    @Test\r\n    public void test4() throws IOException {\r\n        \/\/1.\r\n        DataInputStream dis = new DataInputStream(new FileInputStream(\"data.txt\"));\r\n        \/\/2.\r\n        String name = dis.readUTF();\r\n        int age = dis.readInt();\r\n        boolean isMale = dis.readBoolean();\r\n\r\n        System.out.println(\"name = \" + name);\r\n        System.out.println(\"age = \" + age);\r\n        System.out.println(\"isMale = \" + isMale);\r\n\r\n        \/\/3.\r\n        dis.close();\r\n    }\r\n}\r\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>package top.qaqaq.java.P603;\r\n\/\/ MyInput.java: Contain the methods for reading int, double, float, boolean, short, byte and\r\n\/\/ string values from the keyboard\r\n\r\nimport java.io.*;\r\n\r\npublic class MyInput {\r\n    \/\/ Read a string from the keyboard\r\n    public static String readString() {\r\n        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n        \/\/ Declare and initialize the string\r\n        String string = \"\";\r\n\r\n        \/\/ Get the string from the keyboard\r\n        try {\r\n            string = br.readLine();\r\n\r\n        } catch (IOException ex) {\r\n            System.out.println(ex);\r\n        }\r\n\r\n        \/\/ Return the string obtained from the keyboard\r\n        return string;\r\n    }\r\n\r\n    \/\/ Read an int value from the keyboard\r\n    public static int readInt() {\r\n        return Integer.parseInt(readString());\r\n    }\r\n\r\n    \/\/ Read a double value from the keyboard\r\n    public static double readDouble() {\r\n        return Double.parseDouble(readString());\r\n    }\r\n\r\n    \/\/ Read a byte value from the keyboard\r\n    public static double readByte() {\r\n        return Byte.parseByte(readString());\r\n    }\r\n\r\n    \/\/ Read a short value from the keyboard\r\n    public static double readShort() {\r\n        return Short.parseShort(readString());\r\n    }\r\n\r\n    \/\/ Read a long value from the keyboard\r\n    public static double readLong() {\r\n        return Long.parseLong(readString());\r\n    }\r\n\r\n    \/\/ Read a float value from the keyboard\r\n    public static double readFloat() {\r\n        return Float.parseFloat(readString());\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-1323","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\/1323"}],"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=1323"}],"version-history":[{"count":1,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1323\/revisions"}],"predecessor-version":[{"id":1324,"href":"https:\/\/qaqaq.top\/index.php?rest_route=\/wp\/v2\/posts\/1323\/revisions\/1324"}],"wp:attachment":[{"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qaqaq.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}