Der er flere biblioteker. Her er to eksempler:
❐ Apache Commons Lang
Apache Commons Lang inkluderer en speciel klasse til at escape eller unescape strenge (CSV, EcmaScript, HTML, Java, Json, XML):org.apache.commons.lang3.StringEscapeUtils
.
-
Undslip til 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""."""
-
Ungå fra 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"."
* Du kan downloade det fra her.
❐ OpenCSV
Hvis du bruger OpenCSV , behøver du ikke at bekymre dig om at undslippe eller undslippe, kun for at skrive eller læse indholdet.
-
Skriver fil:
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();
-
Læser fil:
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();
* Du kan downloade det fra her.