Skip to content

Instantly share code, notes, and snippets.

@pgtwitter
Created February 22, 2015 18:21
Show Gist options
  • Save pgtwitter/c03c2572742c4931c41e to your computer and use it in GitHub Desktop.
Save pgtwitter/c03c2572742c4931c41e to your computer and use it in GitHub Desktop.
任意のカラムだけ抜き出したCSVファイルのバイト列を作る (OpenCSVを用いた)
//import au.com.bytecode.opencsv.CSVReader;
//import au.com.bytecode.opencsv.CSVWriter;
String encoding= "UTF-8";
Reader in = new InputStreamReader(new FileInputStream("/path/to/csvfile.csv"), encoding);
ByteArrayOutputStream ret = new ByteArrayOutputStream();
Writer out = new OutputStreamWriter(ret, encoding);
CSVWriter writer = new CSVWriter(out, ',', '"', '"', "\n");
CSVReader reader = new CSVReader(in);
String[] e;
while ((e = reader.readNext()) != null) {
writer.writeNext(new String[] {
e[0]
});
}
reader.close();
writer.close();
byte[] buf= ret.toByteArray();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment