{"id":666,"date":"2015-05-20T09:36:36","date_gmt":"2015-05-20T01:36:36","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=666"},"modified":"2026-08-18T12:09:33","modified_gmt":"2026-08-18T04:09:33","slug":"java%e4%b8%ad%e6%96%87%e4%bb%b6%e4%b8%8e%e7%9b%ae%e5%bd%95%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=666","title":{"rendered":"Java\u4e2d\u6587\u4ef6\u4e0e\u76ee\u5f55\u7ba1\u7406"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">\u76ee\u5f55\u662f\u7ba1\u7406\u6587\u4ef6\u7684\u7279\u6b8a\u673a\u5236\uff1a\u540c\u7c7b\u6587\u4ef6\u653e\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u65e2\u80fd\u7b80\u5316\u7ba1\u7406\uff0c\u4e5f\u80fd\u63d0\u9ad8\u6548\u7387\u3002Java \u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">java.io<\/code> \u5305\u91cc\u63d0\u4f9b\u4e86 <strong style=\"color: #4870ac;\">File<\/strong> \u7c7b\uff0c\u4e13\u95e8\u7528\u6765\u7ba1\u7406\u78c1\u76d8\u4e0a\u7684\u6587\u4ef6\u548c\u76ee\u5f55\u3002<\/p>\n<p><!--more--><\/p>\n<p style=\"margin: 1em 0px;\">\u6bcf\u4e2a File \u5bf9\u8c61\u4ee3\u8868\u4e00\u4e2a\u6587\u4ef6\u6216\u76ee\u5f55\uff0c\u5bf9\u8c61\u5c5e\u6027\u91cc\u5e26\u7740\u8def\u5f84\u3001\u5927\u5c0f\u3001\u6743\u9650\u7b49\u4fe1\u606f\u3002\u8c03\u7528\u5b83\u7684\u65b9\u6cd5\u53ef\u4ee5\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\uff0c\u5224\u65ad\u8bfb\u5199\u6743\u9650\u548c\u662f\u5426\u5b58\u5728\uff0c\u4e5f\u53ef\u4ee5\u67e5\u8be2\u6700\u8fd1\u4fee\u6539\u65f6\u95f4\u3002\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u7cfb\u7edf\u4e0d\u4e00\u6837\uff0c\u7528 File \u5c31\u80fd\u4ee5\u5e73\u53f0\u65e0\u5173\u7684\u65b9\u5f0f\u7edf\u4e00\u5904\u7406\u3002<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u521b\u5efa File \u5bf9\u8c61<\/h2>\n<p style=\"margin: 1em 0px;\">\u521b\u5efa File \u5bf9\u8c61\u65f6\u9700\u8981\u7ed9\u51fa\u5bf9\u5e94\u7684\u6587\u4ef6\u540d\u6216\u76ee\u5f55\u540d\u3002\u5e38\u7528\u6784\u9020\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u6784\u9020\u65b9\u6cd5<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">public File(String path)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6307\u5b9a\u5173\u8054\u7684\u6587\u4ef6\u6216\u76ee\u5f55\u540d\uff0cpath \u53ef\u542b\u8def\u5f84<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">public File(String path, String name)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4ee5 path \u4e3a\u8def\u5f84\uff0c\u4ee5 name \u4e3a\u6587\u4ef6\u6216\u76ee\u5f55\u540d<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">public File(File dir, String name)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7528\u5df2\u6709 File \u5bf9\u8c61 dir \u4f5c\u4e3a\u76ee\u5f55\uff0c\u518d\u62fc name<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">public File(URI uri)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u7528\u7ed9\u5b9a URI \u5b9a\u4f4d\u6587\u4ef6\uff08\u539f\u6587 URL \u6784\u9020\u5df2\u4e0d\u63a8\u8350\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p style=\"margin: 1em 0px;\">\u4f7f\u7528\u65f6\u6ce8\u610f\u4e24\u70b9\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">path<\/code> \u53ef\u4ee5\u662f\u7edd\u5bf9\u8def\u5f84\u3001\u76f8\u5bf9\u8def\u5f84\uff0c\u4e5f\u53ef\u4ee5\u662f\u67d0\u4e2a\u76ee\u5f55\u3002<\/section>\n<\/li>\n<li>\n<section>\u4e0d\u540c\u64cd\u4f5c\u7cfb\u7edf\u7684\u76ee\u5f55\u5206\u9694\u7b26\u4e0d\u540c\u3002\u539f\u6587\u5199\u7684 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">System.dirSep<\/code> \u5e76\u4e0d\u5b58\u5728\uff0c\u5e94\u4f7f\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">File.separator<\/code>\u3002<\/section>\n<\/li>\n<\/ul>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\"d:\" + File.separator + \"myjava\" + File.separator + \"file\"<\/code><\/pre>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001\u83b7\u53d6\u5c5e\u6027\u4e0e\u64cd\u4f5c<\/h2>\n<p style=\"margin: 1em 0px;\">\u501f\u52a9 File \u5bf9\u8c61\u53ef\u4ee5\u67e5\u8be2\u5c5e\u6027\uff0c\u4e5f\u53ef\u4ee5\u521b\u5efa\u3001\u5220\u9664\u3001\u91cd\u547d\u540d\uff1a<\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u65b9\u6cd5<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">boolean canRead() \/ canWrite()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u662f\u5426\u53ef\u8bfb \/ \u53ef\u5199<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">boolean exists()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6587\u4ef6\u6216\u76ee\u5f55\u662f\u5426\u5b58\u5728<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">boolean createNewFile()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4e0d\u5b58\u5728\u5219\u521b\u5efa\u7a7a\u6587\u4ef6\u5e76\u8fd4\u56de true\uff1b\u5df2\u5b58\u5728\u8fd4\u56de false<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">boolean isFile() \/ isDirectory()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u662f\u5426\u4e3a\u6709\u6548\u6587\u4ef6 \/ \u76ee\u5f55<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">String getName() \/ getPath()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u8fd4\u56de\u540d\u79f0 \/ \u8def\u5f84\u5b57\u7b26\u4e32<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">long length() \/ lastModified()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5b57\u8282\u6570\uff08\u76ee\u5f55\u4e3a 0\uff09\/ \u6700\u8fd1\u4fee\u6539\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">String[] list()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u76ee\u5f55\u4e0b\u6240\u6709\u6587\u4ef6\u540d\uff1b\u4e0d\u662f\u76ee\u5f55\u5219\u8fd4\u56de null<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">boolean delete() \/ mkdir()<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u5220\u9664\uff08\u7a7a\u76ee\u5f55\u624d\u80fd\u5220\uff09\/ \u521b\u5efa\u5b50\u76ee\u5f55<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">boolean renameTo(File newFile)<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u91cd\u547d\u540d\u4e3a\u6307\u5b9a\u6587\u4ef6\u540d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u5224\u65ad\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55<\/h2>\n<p style=\"margin: 1em 0px;\">\u3010\u4f8b 10-11\u3011\u5224\u65ad\u8f93\u5165\u7684\u7edd\u5bf9\u8def\u5f84\u662f\u6587\u4ef6\u8fd8\u662f\u76ee\u5f55\u3002\u82e5\u662f\u6587\u4ef6\uff0c\u8f93\u51fa\u7edd\u5bf9\u8def\u5f84\u5e76\u5224\u65ad\u662f\u5426\u53ef\u8bfb\u5199\u3001\u662f\u5426\u9690\u85cf\uff1b\u82e5\u662f\u76ee\u5f55\uff0c\u8f93\u51fa\u8be5\u76ee\u5f55\u4e0b\u6240\u6709\u975e\u9690\u85cf\u6587\u4ef6\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import java.io.*;\nclass ep10_11 {\n    public static void main(String args[]) throws IOException {\n        InputStreamReader in = new InputStreamReader(System.in);\n        BufferedReader a = new BufferedReader(in);\n        System.out.println(\"\u8bf7\u8f93\u5165\u4e00\u4e2a\u7edd\u5bf9\u8def\u5f84\uff1a\");\n        String FilePath = a.readLine();\n        File FileName = new File(FilePath);\n        if (FileName.isDirectory()) {\n            System.out.println(FileName.getName() + \"\u4e3a\u4e00\u4e2a\u76ee\u5f55\");\n            System.out.println(\"================\");\n            File FileList[] = FileName.listFiles();\n            for (int i = 0; i < FileList.length; i++) {\n                if (FileList[i].isHidden() == false) {\n                    System.out.println(FileList[i].getName());\n                }\n            }\n        } else {\n            System.out.println(FileName.getName() + \"\u4e3a\u4e00\u4e2a\u6587\u4ef6\");\n            System.out.println(\"================\");\n            System.out.println(\"\u7edd\u5bf9\u8def\u5f84\u4e3a\uff1a\" + FileName.getAbsolutePath());\n            System.out.println(FileName.canRead() ? \"\u53ef\u8bfb\u53d6\" : \"\u4e0d\u53ef\u8bfb\u53d6\");\n            System.out.println(FileName.canWrite() ? \"\u53ef\u4fee\u6539\" : \"\u4e0d\u53ef\u4fee\u6539\");\n            System.out.println(FileName.isHidden() ? \"\u4e3a\u9690\u85cf\u6587\u4ef6\" : \"\u975e\u9690\u85cf\u6587\u4ef6\");\n        }\n    }\n}<\/code><\/pre>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aFile \u53ea\u63cf\u8ff0\u8def\u5f84\uff0c\u4e0d\u8d1f\u8d23\u8bfb\u5199\u5185\u5bb9\uff1b\u7528\u5b83\u521b\u5efa\u3001\u67e5\u8be2\u3001\u5220\u9664\u6587\u4ef6\u548c\u76ee\u5f55\uff0c\u8de8\u5e73\u53f0\u65f6\u7528 File.separator \u62fc\u8def\u5f84\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u5f55\u662f\u7ba1\u7406\u6587\u4ef6\u7684\u7279\u6b8a\u673a\u5236\uff1a\u540c\u7c7b\u6587\u4ef6\u653e\u5728\u540c\u4e00\u76ee\u5f55\u4e0b\uff0c\u65e2\u80fd\u7b80\u5316\u7ba1\u7406\uff0c\u4e5f\u80fd\u63d0\u9ad8\u6548\u7387\u3002Java \u5728 java.io \u5305 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[22],"tags":[41],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/666"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=666"}],"version-history":[{"count":1,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions"}],"predecessor-version":[{"id":667,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/666\/revisions\/667"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}