package sample;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.StringTokenizer;

public class ReadCSV {

	public static final int NUMBER_STATE = 0;
	public static final int ACTIVE_STATE = 1;
	public static final int PASSWORD_STATE = 2;
	public static final int AREA_STATE = 3;
	public static final int ID_TYPE_STATE = 4;
	public static final int USER_ID_STATE = 5;
	public static final int USER_NAME_STATE = 6;
	public static final int USER_DIVISION_STATE = 7;

	public static void main(String[] args) {
		// TODO ꂽ\bhEX^u

		String test_st = "";

		try {
			File csv = new File("writers.csv"); // CSVf[^t@C

			BufferedReader br = new BufferedReader(new FileReader(csv));

			// ŏIs܂œǂݍ
			String line = "";

			String st_number = "";
			String st_active = "";
			String st_password = "";
			String st_area = "";
			String st_id_type = "";
			String st_user_id = "";
			String st_user_name = "";
			String st_user_division = "";
			String st_test = "";

			int i = 0;
			int card_num = 0;

			byte[] bytes = new byte[604000]; // Mobt@

			while ((line = br.readLine()) != null) {

				// 1sf[^̗vfɕ
				StringTokenizer st = new StringTokenizer(line, ",");

				while (st.hasMoreTokens()) {

					switch (i) {
					case NUMBER_STATE:
						st_number = st.nextToken();
						number_state(st_number, bytes, card_num);
						break;
					case ACTIVE_STATE:
						st_active = st.nextToken();
						break;
					case PASSWORD_STATE:
						st_password = st.nextToken();
						break;
					case AREA_STATE:
						st_area = st.nextToken();
						break;
					case ID_TYPE_STATE:
						st_id_type = st.nextToken();
						break;
					case USER_ID_STATE:
						st_user_id = st.nextToken();
						break;
					case USER_NAME_STATE:
						st_user_name = st.nextToken();
						break;
					case USER_DIVISION_STATE:
						st_user_division = st.nextToken();
						break;
					default:
						st_test = st.nextToken();
						break;
					}

					i++;
					// byte[] bytes = test_st.getBytes("UTF-8");
					// System.out.print(test_st);
				}
				test_st = test_st + st_number + "\t" + st_active + "\t"
						+ st_password + "\t" + st_area + "\t" + st_id_type
						+ "\t" + st_user_id + "\t" + st_user_name + "\t"
						+ st_user_division + "\n";
				i = 0;
				card_num++;
			}
			br.close();
			System.out.print(test_st);

		} catch (FileNotFoundException e) {
			// FileIuWFNg̗Oߑ
			e.printStackTrace();
		} catch (IOException e) {
			// BufferedReaderIuWFNg̃N[Y̗Oߑ
			e.printStackTrace();
		}
	}

	private static void number_state(String st_number, byte[] bytes, int card_num)
			throws UnsupportedEncodingException {
		// TODO ꂽ\bhEX^u

		byte[] get_number = st_number.getBytes("UTF-8");
		for (int j = 0; j < get_number.length; j++) {
			bytes[0 + (card_num * 50) + j] = get_number[j];
			// bytes = get_number[j];
		}

	}

	/**
	 * oCif[^t@Cɏ݂܂B
	 * 
	 * @param bytess
	 */
	public void write(byte[][] bytess) {
		BufferedOutputStream fis = null;
		try {
			// o͐t@C
			File file = new File("C:/Winter_clone.jpg");

			fis = new BufferedOutputStream(new FileOutputStream(file));
			for (byte[] bytes : bytess) {
				fis.write(bytes);
			}

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fis != null) {
					// Xg[͕K finally  close ܂B
					fis.close();
				}
			} catch (IOException e) {
			}
		}
	}

}
