5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SuperCSVメモ

Posted at

特徴

SuperCSVはBeanマッピングができる

例1 csvファイルを読み込む

プロジェクト配下に以下を作成
/resources/sample.csv
skitch.png

Main.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.constraint.StrMinMax;
import org.supercsv.cellprocessor.constraint.StrRegEx;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvBeanReader;
import org.supercsv.io.ICsvBeanReader;
import org.supercsv.prefs.CsvPreference;

public class Main {
	static String CSV_FILENAME = System.getProperty("user.dir") + File.separator + "resources" + File.separator + "sample.csv";

	public static void main(String[] args) {
		try {
			ICsvBeanReader beanReader = new CsvBeanReader(new InputStreamReader(new FileInputStream(new File(CSV_FILENAME)), "SJIS"), CsvPreference.EXCEL_PREFERENCE);
			try {
				final String[] header = beanReader.getHeader(true);
				final CellProcessor[] processors = getProcessors();
				User user;
				while ((user = beanReader.read(User.class, header, processors)) != null) {
					System.out.println(
							String.format("行数=%s, user=[%s]", beanReader.getLineNumber(), user));
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

	}

	private static CellProcessor[] getProcessors() {
		final String emailRegex = "[a-z0-9\\._]+@[a-z0-9\\.]+"; // just an example, not very robust!
		StrRegEx.registerMessage(emailRegex, "must be a valid email address");

		final CellProcessor[] processors = new CellProcessor[] {
				new StrMinMax(6, 20), // id 6桁から20桁の間
				new NotNull(), // name
				new StrRegEx(emailRegex) // email
		};

		return processors;
	}
}

User.java
public class User {
	private String id;
	private String name;
	private String email;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String toString() {
		return "id=" + id + ",name=" + name;
	}
}

結果

行数=2, user=[id=123456,name=zakki]
行数=3, user=[id=895623,name=tom]

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?