{"id":1881,"date":"2021-04-20T16:56:53","date_gmt":"2021-04-20T07:56:53","guid":{"rendered":"https:\/\/esoro.jp\/?p=1881"},"modified":"2021-04-20T16:56:53","modified_gmt":"2021-04-20T07:56:53","slug":"apache-commons-csv%e3%81%a7invalid-char-between-encapsulated-token-and-delimiter","status":"publish","type":"post","link":"https:\/\/esoro.jp\/?p=1881","title":{"rendered":"apache.commons.csv\u3067invalid char between encapsulated token and delimiter"},"content":{"rendered":"<p>\u3000\u3060\u3044\u3076\u524d\u306b\u4f5c\u3063\u305fapache.commons.csv\u3068apache.poi\u3092\u4f7f\u3063\u305fCSV\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u30a8\u30af\u30bb\u30eb\u51fa\u529b\u51e6\u7406\u3067\u3001<\/p>\n<p>java.io.IOException: (line 3) invalid char between encapsulated token and delimiter<\/p>\n<p>\u3000\u304c\u767a\u751f\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u539f\u56e0\u306f\u3001\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u62ec\u3063\u3066\u3044\u308b\u30c7\u30fc\u30bf\u9805\u76ee\u306e\u4e2d\u306b\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u6df7\u3056\u3063\u3066\u3044\u305f\u304b\u3089\u306a\u306e\u3067\u3059\u304c\u3001\u30c7\u30fc\u30bf\u3092\u51fa\u529b\u3059\u308b\u5074\u3067\u5bfe\u51e6\u3059\u308b\u306e\u304c\u51fa\u6765\u305d\u3046\u306b\u306a\u3044\u306e\u3067\u3001CSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u307f\u8fbc\u3080\u6240\u3067\u4f55\u3068\u304b\u306a\u3089\u3093\u3082\u306e\u304b\u3068\u691c\u8a0e\u3002<br \/>\n\u3000<br \/>\n\u3000CSV\u30d5\u30a1\u30a4\u30eb\u3092\u8aad\u3080\u7b87\u6240\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306bInputStream\u304c\u6e21\u3055\u308c\u3001CSVRecord\u30ea\u30b9\u30c8\u3092\u53d6\u5f97\u3057\u3066\u8272\u3005\u3084\u308b\u3068\u3044\u3046\u5185\u5bb9\u3067\u3059\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"CSVproc.java\" >    public static int parseCSV(InputStream input){\r\n\tBufferedReader in = new BufferedReader(new InputStreamReader(input));\r\n\r\n\tCSVParser parser = CSVFormat\r\n\t        .EXCEL\r\n\t        .withIgnoreEmptyLines(true)\r\n\t        .withIgnoreSurroundingSpaces(false)\r\n\t        .withRecordSeparator(System.getProperty(\"line.separator\"))\r\n\t        .withDelimiter(',')\r\n\t        .withEscape('\\\\')\r\n\t        .withQuote('\"')\r\n\t        .parse(in);\r\n\tList&lt;CSVRecord&gt; recordList = parser.getRecords();\r\n\r\n\/\/\u4ee5\u4e0b\u3001\u7565<\/pre>\n<p>\u3000\u6b86\u3069\u306e\u5834\u5408\u3067\u554f\u984c\u306f\u767a\u751f\u3057\u306a\u3044\u306e\u3067\u3001\u51fa\u6765\u308c\u3070\u5931\u6557\u3057\u305f\u5834\u5408\u306e\u307f\u30ea\u30c8\u30e9\u30a4\u51fa\u6765\u308c\u3070\u3044\u3044\u304b\u306a\u3068\u3001\u30c7\u30fc\u30bf\u5185\u306b\u6df7\u3056\u308a\u3053\u3093\u3060\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3092\u30a8\u30b9\u30b1\u30fc\u30d7\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3059\u3002\u6b63\u898f\u8868\u73fe\u3067\u306e\u5909\u63db\u3092\uff13\u6bb5\u968e\u304b\u307e\u3059\u3053\u3068\u3067\u3001\u4e00\u90e8\u30b1\u30fc\u30b9\uff08\u30c7\u30fc\u30bf\u5185\u306b\u30ab\u30f3\u30de\u3068\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u304c\u9023\u7d9a\u3057\u305f\u5834\u5408\uff09\u306f\u554f\u984c\u3067\u3059\u304c\u3001\u4eca\u56de\u306f\u307e\u3042\u3053\u306e\u4f4d\u3067\u3002\u3002<\/p>\n<p>\u3000\u6b21\u306b\u5931\u6557\u3057\u305f\u5f8c\u306e\u30d5\u30a9\u30ed\u30fc\u3068\u306a\u308b\u306e\u3067\u3001\u8aad\u8fbc\u304c\u9032\u3093\u3067\u3057\u307e\u3063\u3066\u3044\u308bInputStream\u3092\u4f55\u3068\u304b\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u7d50\u5c40\u3001IOUtils\u3067\u30c6\u30f3\u30dd\u30e9\u30ea\u30d5\u30a1\u30a4\u30eb\u306b\u30b3\u30d4\u30fc\u3057\u3066\u304b\u3089\u8aad\u307f\u8fbc\u3080\u3088\u3046\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"lang:java decode:true \" title=\"CSVproc.java\" >    public static int parseCSV(InputStream input){\r\n\/\/\tBufferedReader in = new BufferedReader(new InputStreamReader(input));\r\n\r\n        File fstream = File.createTempFile(\"csv\",\".txt\");\r\n        FileOutputStream out = new FileOutputStream(fstream);\r\n        IOUtils.copy(input, out);\r\n        out.close();\r\n\r\n        List&lt;CSVRecord&gt; recordList;\r\n\r\n        try(BufferedInputStream bf = new BufferedInputStream(new\u3000FileInputStream(fstream.getAbsoluteFile()));\r\n            BufferedReader in = new BufferedReader(new InputStreamReader(bf));)\r\n        {\r\n            CSVParser parser = CSVFormat\r\n                    .EXCEL\r\n                    .withIgnoreEmptyLines(true)\r\n                    .withIgnoreSurroundingSpaces(false)\r\n                    .withRecordSeparator(System.getProperty(\"line.separator\"))\r\n                    .withDelimiter(',')\r\n                    .withEscape('\\\\')\r\n                    .withQuote('\"')\r\n                    .parse(in);\r\n\r\n            recordList = parser.getRecords();\r\n        }\r\n        catch(Exception e){\r\n            logger.error(\"retry parse tmpfile:\" + fstream.getAbsoluteFile(),e );\r\n            BufferedInputStream bf = new BufferedInputStream(new FileInputStream(fstream.getAbsoluteFile()));\r\n            BufferedReader in = new BufferedReader(new InputStreamReader(bf,\"UTF-8\"));\r\n            StringBuilder sb = new StringBuilder();\r\n            String line;\r\n            while ((line = in.readLine()) != null) {\r\n                line.replaceAll(\"\\\\\\\\\", \"\");\r\n                line = line.replaceAll(\"(?!(^|,|\\\",))\\\"(?!$|\\\")\",\"\\\\\\\\\\\"\");\r\n                line = line.replaceAll(\",\\\\\\\\\\\"\",\",\\\"\");\r\n                sb.append(line+System.getProperty(\"line.separator\"));\r\n            }\r\n            CSVParser parser = CSVFormat\r\n                    .EXCEL\r\n                    .withIgnoreEmptyLines(true)\r\n                    .withIgnoreSurroundingSpaces(false)\r\n                    .withRecordSeparator(System.getProperty(\"line.separator\"))\r\n                    .withDelimiter(',')\r\n                    .withEscape('\\\\')\r\n                    .withQuote('\"')\r\n                    .parse(new StringReader(sb.toString()));\r\n\r\n            recordList = parser.getRecords();\r\n            bf.close();\r\n            in.close();\r\n        }\r\n        fstream.deleteOnExit();\r\n\/\/\u4ee5\u4e0b\u3001\u7565<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3060\u3044\u3076\u524d\u306b\u4f5c\u3063\u305fapache.commons.csv\u3068apache.poi\u3092\u4f7f &hellip; <a href=\"https:\/\/esoro.jp\/?p=1881\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,2],"tags":[],"_links":{"self":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1881"}],"collection":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1881"}],"version-history":[{"count":3,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1881\/revisions"}],"predecessor-version":[{"id":1884,"href":"https:\/\/esoro.jp\/index.php?rest_route=\/wp\/v2\/posts\/1881\/revisions\/1884"}],"wp:attachment":[{"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/esoro.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}