{"id":361,"date":"2023-02-12T13:10:13","date_gmt":"2023-02-12T05:10:13","guid":{"rendered":"https:\/\/www.gladguang.cn\/?p=361"},"modified":"2023-02-12T13:10:13","modified_gmt":"2023-02-12T05:10:13","slug":"gbk%e6%96%87%e4%bb%b6%e6%a0%bc%e5%bc%8f%e6%89%b9%e9%87%8f%e8%bd%ac%e6%8d%a2utf-8","status":"publish","type":"post","link":"https:\/\/www.gladguang.cn\/index.php\/2023\/02\/12\/gbk%e6%96%87%e4%bb%b6%e6%a0%bc%e5%bc%8f%e6%89%b9%e9%87%8f%e8%bd%ac%e6%8d%a2utf-8\/","title":{"rendered":"GBK\u6587\u4ef6\u683c\u5f0f\u6279\u91cf\u8f6c\u6362UTF-8"},"content":{"rendered":"<pre><code class=\"language-java line-numbers\">import java.io.BufferedReader;\nimport java.io.BufferedWriter;\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.OutputStreamWriter;\n\npublic class Main {\n    \/\/\u65e7\u6587\u4ef6(GBK\u683c\u5f0f)\u8def\u5f84\u3002\u53ef\u4ee5\u662f\u6574\u4e2a\u76ee\u5f55\u4e5f\u53ef\u4ee5\u5355\u4e2a\u6587\u4ef6\u8f6c\u6362\n    private String sDirectory = \"\/root\/Linux\/files\/2\";\n    \/\/\u65b0\u6587\u4ef6(UTF-8)\u8def\u5f84\n    private String dDirectory = \"\/root\/Linux\/files\/22\";\n\n    public static void main(String[] args) {\n        Main convert = new Main();\n        try {\n            convert.readerFile(a.sDirectory);\n        } catch (IOException e) {\n            convert.printStackTrace();\n        }\n    }\n\n    public void readerFile(String filePath) throws IOException {\n        if (\"\".equals(filePath) || null == filePath) {\n            return;\n        }\n\n        File f = new File(filePath);\n        if (f.isDirectory()) {\n            String[] child = f.list();\n            for (int i = 0; i &lt; child.length; i++) {\n                String path = f.getAbsolutePath() + File.separator;\n                String newPath = path.replace(this.sDirectory, this.dDirectory);\n                child[i] = path + child[i];\n                File c = new File(child[i]);\n                String newFile = child[i].replace(this.sDirectory, this.dDirectory);\n                System.out.println(\"\u65e7\u8def\u5f84\uff1a\" + path);\n                System.out.println(\"\u65b0\u8def\u5f84\uff1a\" + newPath);\n\n                File newP = new File(newPath);\n                if (!newP.exists())\n                    newP.mkdir();\n\n                if (c.isFile()) {\n                    System.out.println(\"\u65e7\u6587\u4ef6\uff1a\" + child[i]);\n                    System.out.println(\"\u65b0\u6587\u4ef6\uff1a\" + newFile);\n                    \/\/ Charset US-ASCII ISO-8859-1 UTF-8 UTF-16BE UTF-16LE UTF-16\n                    BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(c), \"GBK\"));\n                    \/\/ BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(c)));\n                    File newF = new File(newFile);\n                    newF.createNewFile();\n                    \/\/ BufferedWriter w = new BufferedWriter(new\n                    \/\/ OutputStreamWriter(new FileOutputStream(newF), \"UTF-8\"));\n                    BufferedWriter w = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newF)));\n                    \/\/ BufferedWriter w = new BufferedWriter(new FileWriter(newFile));\n                    String lineText = null;\n                    while ((lineText = r.readLine()) != null) {\n                        \/\/ String temp = new String(lineText.getBytes(\"ISO-8859-1\"), \"UTF-8\");\n                        w.write(lineText);\n                        w.newLine();\n                    }\n                    w.close();\n                    r.close();\n                } else {\n                    readerFile(child[i]);\n                }\n            }\n        }\n    }\n\n}\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7528Java\u5b9e\u73b0GBK\u6587\u4ef6\u683c\u5f0f\u6279\u91cf\u8f6c\u6362\u6210UTF-8<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,64,49],"tags":[52,71,72],"class_list":["post-361","post","type-post","status-publish","format-standard","hentry","category-59","category-64","category-java","tag-java","tag-71","tag-72"],"_links":{"self":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/comments?post=361"}],"version-history":[{"count":1,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions"}],"predecessor-version":[{"id":362,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/posts\/361\/revisions\/362"}],"wp:attachment":[{"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/media?parent=361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/categories?post=361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gladguang.cn\/index.php\/wp-json\/wp\/v2\/tags?post=361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}