{"id":1633,"date":"2020-10-20T22:38:54","date_gmt":"2020-10-20T14:38:54","guid":{"rendered":"https:\/\/199604.com\/?p=1633"},"modified":"2020-10-20T22:39:43","modified_gmt":"2020-10-20T14:39:43","slug":"%e4%bd%bf%e7%94%a8bufferedreader%e5%92%8cbufferedwriter%e8%bd%ac%e6%8d%a2%e6%96%87%e4%bb%b6%e7%bc%96%e7%a0%81%e6%a0%bc%e5%bc%8f%e9%81%87%e5%88%b0%e7%9a%84%e5%9d%91","status":"publish","type":"post","link":"https:\/\/199604.com\/1633","title":{"rendered":"\u4f7f\u7528BufferedReader\u548cBufferedWriter\u8f6c\u6362\u6587\u4ef6\u7f16\u7801\u683c\u5f0f\u9047\u5230\u7684\u5751~"},"content":{"rendered":"<p>\u6700\u8fd1\u4e00\u4e2a\u7701\u7684\u5bfc\u51fa\u529f\u80fd\u9047\u5230\u4e86\u4e00\u4e2a\u5947\u8469\u7684BUG\uff0c\u81ea\u4e2a\u6298\u817e\u4e86\u5f88\u4e45\u5f88\u65e0\u52a9\uff0c\u5148\u8bb0\u5f55\u5427~<\/p>\n<p>\u542f\u521d\u8fd8\u4ee5\u4e3a\u662f\u90e8\u7f72\u7684\u4ee3\u7801\u7248\u672c\u8fc7\u4e45\u5bfc\u81f4\u7684\uff0c\u4e8e\u662f\u53c8\u91cd\u65b0\u53bb\u90e8\u7f72\u4e00\u4efd\u6700\u65b0\u4ee3\u7801\u8fd8\u662f\u65e0\u6cd5\u89e3\u51b3\u3002\u56e0\u4e3a\u5bfc\u51fa\u529f\u80fd\u662f\u5148\u4ecegp\u6570\u636e\u5e93\u4e0acopy\u51fa UTF-8\u7684csv\u6587\u4ef6,\u57fa\u672c\u4e0a\u5f88\u5feb\u751f\u6210\u3002\u53ef\u662f\u5230\u4e86\u9700\u8981\u5230\u6570\u636e\u8f6c\u6362\u6210GBK\u7f16\u7801\u683c\u5f0f\u65f6\u5019\u5c31\u51fa\u95ee\u9898\u4e86\u3002<\/p>\n<p>BUG\u662f\u8fd9\u6837\u7684\uff0c\u5f00\u59cb\u5bfc\u51fa\u5e76\u4e14\u8f6c\u6362\u5c0f\u6570\u636e\u91cf\u5462\u57fa\u672c\u4e0a\u83ab\u5f97\u95ee\u9898\uff0c\u53ef\u5230\u4e8650W\u6761\u6570\u636e\u4ee5\u540e\u5c31\u53d8\u6210\u5047\u6b7b\u72b6\u6001\u3002<\/p>\n<p>\u8d77\u521d\u6211\u770b\u4ee3\u7801\u90fd\u53d1\u73b0\u4e0d\u4e86\u95ee\u9898\u6240\u5728\uff0c\u89c9\u5f97\u4ee3\u7801\u5728\u51e0\u4e2a\u6bd4\u8f83\u5927\u7684\u7701\u4efd\u90fd\u80fd\u8dd1\u8d77\u6765~\u6570\u636e\u91cf\u57fa\u672c\u4e0a\u90fd\u5728\u767e\u4e07\u57fa\u672c\u4e0a\u3002<\/p>\n<p>\u8fdb\u800c\u6211\u5206\u4e86\u8fd9\u8fd9\u51e0\u6b65\u6392\u9664\uff1a<\/p>\n<p>1.\u68c0\u67e5\u8df3\u8f6ctomcat\u7684jvm\u53c2\u6570\/\u6216\u8005server.xml\u662f\u5426\u6709\u5b58\u5728\u9650\u5236&#8212;\u6b63\u5e38<\/p>\n<p>2.\u68c0\u67e5\u662f\u5426\u662f\u670d\u52a1\u5668\u7684\u7f13\u51b2\u533a\u8bbe\u7f6e\u95ee\u9898<\/p>\n<pre><code>cat \/proc\/sys\/vm\/drop_caches \r\n<\/code><\/pre>\n<p>\u53d1\u73b0\/proc\/sys\/vm\/drop_caches \u72b6\u6001\u4e3a3\uff0c\u4e14crontab \u5b9a\u65f6\u4e5f\u662f1\/3 \u72b6\u6001\u6bcf\u5341\u5206\u949f\u6267\u884c\u4e00\u904d\uff0c\u7981\u6b62\u540e\u8fd8\u662f\u4f1a\u65e0\u6cd5\u751f\u6210 \u8f6c\u6362\u540e\u7684\u6587\u4ef6&#8211;\u5047\u6b7b\u72b6\u6001<\/p>\n<p>3.\u65e2\u7136\u73af\u5883\u90fd\u4fee\u6539\u4e86\uff0c\u8fd8\u662f\u65e0\u6cd5\u6210\u529f\u90a3\u4e48\u7ee7\u7eed\u4ece\u4ee3\u7801\u5b9a\u4f4d\u3002<\/p>\n<p>debug\u5b9a\u4f4d\u5230\u5047\u6b7b\u4ee3\u7801\u5757\uff1a<\/p>\n<pre><code>while ((line = bin.readLine()) != null) {\r\n\tcontent.append(line);\r\n\tcontent.append(System.getProperty(\"line.separator\"));\r\n\tif(content.length()&gt;9999999) {\r\n\t\tlog.info(\"content.length:{}\",content.length());\r\n\t\twriter.write(content.toString());\r\n\t\twriter.flush();\r\n\t\tcontent = new StringBuffer();\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>\u901a\u8fc7\u65e5\u5fd7\u53d1\u73b0\uff0c\u6bcf\u6b21\u5f53 <mark>content.length()<\/mark>\u6ee1\u8db3\u6761\u4ef6\u4e4b\u540e\u5e76\u4e14\u8fdb\u884c\u5199\u5165\u540e\uff0c\u5c31\u5361\u4f4f\u4e86.\u4e14\u65e5\u5fd7\u7684content.length \u53ea\u4f1a\u63d0\u793a\u4e00\u904d\u3002<\/p>\n<p>\u56e0\u6b64\u5224\u65ad<mark>if(content.length()&gt;9999999)<\/mark>\u6761\u4ef6\u5b58\u5728\u4e00\u5b9a\u95ee\u9898\u3002\u53ef\u80fd\u5c31\u662f\u56e0\u4e3acontent\u592a\u5927\uff0c\u7f13\u51b2\u533a\u65e0\u6cd5\u4e00\u6b21\u6027\u5199\u5165\u9020\u6210\u7684\u3002\u4e8e\u662f\u6362\u4e00\u4e2a\u6761\u4ef6\u8fdb\u884c\u5199\u5165\uff1a<\/p>\n<pre><code>int count = 0;\r\nwhile ((line = bin.readLine()) != null) {\r\n\tcount++;\r\n\tcontent.append(line);\r\n\tcontent.append(System.getProperty(\"line.separator\"));\r\n\tif (count%100000==0) {\r\n\t\tlog.info(\"count.length:{}\",count);\r\n\t\twriter.write(content.toString());\r\n\t\twriter.flush();\r\n\t\tcontent = new StringBuffer();\r\n\t}\r\n}\r\n<\/code><\/pre>\n<p>\u5373\u6bcf\u5f53\u904d\u5386\u6761\u6570\u5230\u8fbe\u4e00\u5b9a\u7a0b\u5ea6\u5373\u53ef\u5199\u5165\uff0c\u9632\u6b62\u7f13\u51b2\u533a\u8fc7\u5927\u88ab\u56de\u6536(\u540e\u671f\u904d\u5386\u6761\u6570\u8bbe\u7f6e\u591a\u5c11\u53ef\u5728\u53c2\u6570\u7ba1\u7406\u8bbe\u7f6e)<\/p>\n<p>\u6d4b\u8bd5\u90e8\u7f72\u540e\uff0c\u8f6c\u6362\u751f\u6210\u6587\u4ef6\u8017\u65f6\u662f\u539f\u6765\u76841\/2.\u90a3\u4e48\u89e3\u51b3&#8230;<\/p>\n<p>\u56e0\u6b64\uff0c\u4f7f\u7528BufferedWriter\u5199\u5165\u65f6\u5019\u4e5f\u9700\u8981\u8003\u8651\u7cfb\u7edf\u6027\u80fd\u8bbe\u7f6e\u7684\u95ee\u9898<\/p>\n<p>\u5b8c\u6574\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code>\t\/**\r\n\t * \u5c06\u4e00\u6307\u5b9a\u7f16\u7801\u7684\u6587\u4ef6\u8f6c\u6362\u4e3a\u53e6\u4e00\u7f16\u7801\u7684\u6587\u4ef6\r\n\t *\r\n\t * @param oldFullFileName\r\n\t * @param oldCharsetName\r\n\t * @param newFullFileName\r\n\t * @param newCharsetName\r\n\t *\/\r\n\tpublic static void convert(String oldFullFileName, String oldCharsetName, String newFullFileName, String newCharsetName) {\r\n\t\tlog.info(\"the old file name is : {}, The oldCharsetName is : {}\", oldFullFileName, oldCharsetName);\r\n\t\tlog.info(\"the new file name is : {}, The newCharsetName is : {}\", newFullFileName, newCharsetName);\r\n\t\ttry {\r\n\t\t\tsemaphore.acquire();\/\/ \u83b7\u53d6\u4fe1\u53f7\u91cf,\u5360\u7528\r\n\t\t} catch (InterruptedException e1) {\r\n\t\t   log.error(\"\u6587\u4ef6\u8f6c\u7801\u5e76\u53d1\u5360\u7528\u5f02\u5e38\",e1);\r\n\t\t   return;\r\n\t\t}\r\n\t\ttry (BufferedReader bin = new BufferedReader(new InputStreamReader(new FileInputStream(oldFullFileName), oldCharsetName));\r\n\t\t\t BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFullFileName), newCharsetName))){\r\n\t\t\tStringBuffer content = new StringBuffer();\r\n\r\n\t\t\tString line;\r\n\t\t\tnewFullFileName = newFullFileName.replace(\"\\\\\", \"\/\");\r\n\t\t\tFile dir = new File(newFullFileName.substring(0, newFullFileName.lastIndexOf(\"\/\")));\r\n\t\t\tif (!dir.exists()) {\r\n\t\t\t\tdir.mkdirs();\r\n\t\t\t}\r\n\r\n\t\t\tint count = 0;\r\n\t\t\twhile ((line = bin.readLine()) != null) {\r\n\t\t\t\tcount++;\r\n\t\t\t\tcontent.append(line);\r\n\t\t\t\tcontent.append(System.getProperty(\"line.separator\"));\r\n\t\t\t\tif (count%100000==0) {\r\n\t\t\t\t\tlog.info(\"count.length:{}\",count);\r\n\t\t\t\t\twriter.write(content.toString());\r\n\t\t\t\t\twriter.flush();\r\n\t\t\t\t\tcontent = new StringBuffer();\r\n\t\t\t\t}\r\n\/\/\t\t\t\tif(content.length()&gt;9999999) {\r\n\/\/\t\t\t\t\tlog.info(\"content.length:{}\",content.length());\r\n\/\/\t\t\t\t\twriter.write(content.toString());\r\n\/\/\t\t\t\t\twriter.flush();\r\n\/\/\t\t\t\t\tcontent = new StringBuffer();\r\n\/\/\t\t\t\t\tThread.sleep(1000);\r\n\/\/\t\t\t\t\tlog.info(\"Thread sleep end\");\r\n\/\/\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tlog.info(\"count:{}\",count);\r\n\r\n\t\t\t\/\/write(newFullFileName,content.toString(),newCharsetName);\r\n\r\n\t\t\twriter.write(content.toString());\r\n\t\t\twriter.flush();\r\n\t\t\tlog.info(\"writer---end{}\");\r\n\t\t\tsemaphore.release();\r\n\t\t} catch (Exception e) {\r\n\t\t\tlog.error(\"\u6587\u4ef6\u8f6c\u7801\u5f02\u5e38\",e);\r\n\t\t\tsemaphore.release();\r\n\t\t}\r\n\r\n\t}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4e00\u4e2a\u7701\u7684\u5bfc\u51fa\u529f\u80fd\u9047\u5230\u4e86\u4e00\u4e2a\u5947\u8469\u7684BUG\uff0c\u81ea\u4e2a\u6298\u817e\u4e86\u5f88\u4e45\u5f88\u65e0\u52a9\uff0c\u5148\u8bb0\u5f55\u5427~ \u542f\u521d\u8fd8\u4ee5\u4e3a\u662f\u90e8\u7f72\u7684\u4ee3\u7801\u7248\u672c\u8fc7\u4e45\u5bfc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[130],"tags":[129],"class_list":["post-1633","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/comments?post=1633"}],"version-history":[{"count":3,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1633\/revisions"}],"predecessor-version":[{"id":1636,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/posts\/1633\/revisions\/1636"}],"wp:attachment":[{"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/media?parent=1633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/categories?post=1633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/199604.com\/wp-json\/wp\/v2\/tags?post=1633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}