有几个库。这是两个示例:
❐Apache Commons Lang
阿帕奇共享郎包括一类特殊的逃避或UNESCAPE字符串(CSV,Ecmascript的,HTML,Java和JSON,XML)org.apache.commons.lang3.StringEscapeUtils
。
转义 为CSV
String escaped = StringEscapeUtils.escapeCsv("I said "Hey, I am 5'10".""); // I said "Hey, I am 5'10"."
System.out.println(escaped); // “I said “”Hey, I am 5‘10”“.”“”
__从CSV 取消转义
String unescaped = StringEscapeUtils .unescapeCsv(""I said ""Hey, I am 5'10"".""""); // "I said ""Hey, I am 5'10"".""" System.out.println(unescaped); // I said "Hey, I am 5'10"."
- 您可以从 这里 下载 。
❐OpenCSV
如果使用 OpenCSV ,则无需担心转义或不转义,仅需写或读内容。
- 写入文件:
FileOutputStream fos = new FileOutputStream("awesomefile.csv"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); CSVWriter writer = new CSVWriter(osw); ... String[] row = { "123", "John", "Smith", "39", "I said "Hey, I am 5'10"."" }; writer.writeNext(row); ... writer.close(); osw.close(); os.close();
- 读取文件:
FileInputStream fis = new FileInputStream("awesomefile.csv"); InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); CSVReader reader = new CSVReader(isr); for (String[] row; (row = reader.readNext()) != null;) { System.out.println(Arrays.toString(row)); } reader.close(); isr.close(); fis.close();
- 您可以从 这里 下载 。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)