0
0

More than 1 year has passed since last update.

データからCSVを作る

Posted at
package design.controller;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class TestYou {

	/*CSV出力パス*/
	public static final String CSV_FILE_PATH = "C:\\csv\\";
	/*CSV出力パス*/
	public static final String CSV_FILE_NAME = "CSV_PRINT_TEST";
	/*CSVHeader*/
	public static final String CSV_FILE_HEADER = "ColumnA" + "," + "ColumnB";

	private void makeCsv() {
		File f = new File(CSV_FILE_PATH + CSV_FILE_NAME);
		try {
			FileOutputStream fos = new FileOutputStream(f);
			// BOMつきCSV文字化け防止
			fos.write(0xef);
			fos.write(0xbb);
			fos.write(0xbf);
			OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
			BufferedWriter bw = new BufferedWriter(osw);
			PrintWriter pw = new PrintWriter(bw);
			// CSVのヘッダ作成
			pw.println(CSV_FILE_HEADER);
			// CSVの内容作成
			pw.println("test1" + "," + "test2");
			pw.println("test3" + "," + "test4");

			bw.close();
		} catch (FileNotFoundException e) {
			System.out.println("File Not Found");
		} catch (IOException e) {
			System.out.println("IOException");
		}
	}

}

0
0
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
0
0