java CSVファイルの読み书き
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java CSVファイルの読み书き相关的知识,希望对你有一定的参考价值。
package com.somei.student_management_system.login.domain.model;
import com.opencsv.bean.CsvBindByName;
import lombok.Data;
/**
* 模擬試験のCSVデータを受け取るクラス
*/
@Data
public class ImportPracticeExam {
@CsvBindByName(column = "生徒ID", required = true)
private String studentId;
@CsvBindByName(column = "学年", required = true)
private int grade;
@CsvBindByName(column = "実施年", required = true)
private int examYear;
@CsvBindByName(column = "実施月", required = true)
private String monthName;
@CsvBindByName(column = "英語_点数", required = true)
private String englishScore;
@CsvBindByName(column = "数学_点数", required = true)
private String mathScore;
@CsvBindByName(column = "国語_点数", required = true)
private String japaneseScore;
@CsvBindByName(column = "理科_点数", required = true)
private String scienceScore;
@CsvBindByName(column = "社会_点数", required = true)
private String socialScore;
@CsvBindByName(column = "3科目合計", required = true)
private String sumThree;
@CsvBindByName(column = "5科目合計", required = true)
private String sumAll;
@CsvBindByName(column = "3科目偏差値", required = true)
private String devThree;
@CsvBindByName(column = "5科目偏差値", required = true)
private String devFive;
@CsvBindByName(column = "英語_偏差値", required = true)
private String englishDeviation;
@CsvBindByName(column = "数学_偏差値", required = true)
private String mathDeviation;
@CsvBindByName(column = "国語_偏差値", required = true)
private String japaneseDeviation;
@CsvBindByName(column = "理科_偏差値", required = true)
private String scienceDeviation;
@CsvBindByName(column = "社会_偏差値", required = true)
private String socialDeviation;
}
@CsvBindByName ヘッダーカラムとフィールドを紐付け
@CsvBindByPosition 入力順とフィールドを紐付け
package com.somei.student_management_system.login.bean;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvException;
import com.somei.student_management_system.login.domain.model.ImportPracticeExam;
import org.springframework.stereotype.Component;
import java.io.Reader;
import java.io.Writer;
import java.util.List;
@Component
public class IOCsv {
/**
* CSVファイルの書き込み
*
* @param writer
* @param beans
*/
public void write(Writer writer, List<ImportPracticeExam> beans) throws CsvException {
StatefulBeanToCsv<ImportPracticeExam> beanToCsv = new StatefulBeanToCsvBuilder<ImportPracticeExam>(writer).build();
beanToCsv.write(beans);
}
/**
* CSVファイルの読み取り
*
* @param reader
*/
public List<ImportPracticeExam> read(Reader reader) throws CsvException {
CsvToBean<ImportPracticeExam> csvToBean = new CsvToBeanBuilder<ImportPracticeExam>(reader).withType(ImportPracticeExam.class).build();
return csvToBean.parse();
}
}
package com.somei.student_management_system.login.controller;
import com.opencsv.exceptions.CsvException;
import com.somei.student_management_system.login.bean.IOCsv;
import com.somei.student_management_system.login.bean.excelProcessing;
import com.somei.student_management_system.login.domain.model.ImportPracticeExam;
import com.somei.student_management_system.login.domain.model.SchoolRecordWithName;
import com.somei.student_management_system.login.domain.service.NumericDataService;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
public class RegistryController {
@Autowired
excelProcessing excelProcessing;
@Autowired
NumericDataService numericDataService;
@Autowired
IOCsv ioCsv;
// 途中のメソッド省略
/**
* 模擬試験登録画面のPOSTメソッド.
*
* @param multipartFile 模試のCSVデータ
* @param model モデル
* @return 模試登録確認画面へ遷移
*/
@PostMapping("practiceCsvUpload")
public String postRegistryPracticeExam(@RequestParam("file") MultipartFile multipartFile,
Model model) {
ImportPracticeExam ipe = new ImportPracticeExam();
try (InputStream is = multipartFile.getInputStream();
InputStreamReader ireader = new InputStreamReader(is, "UTF-8");
Reader reader = new BufferedReader(ireader);) {
List<ImportPracticeExam> exams = ioCsv.read(reader);
boolean result = numericDataService.insertPracticeMany(exams);
if (result == true) {
model.addAttribute("result", "模擬試験データを登録しました");
} else {
model.addAttribute("result", "模擬試験データの登録に失敗しました");
}
} catch (IOException e) {
e.printStackTrace();
} catch (CsvException e) {
e.printStackTrace();
} catch (DataAccessException e) {
model.addAttribute("result", "更新失敗(トランザクションテスト)");
}
return getRegistry(model);
}
}
以上是关于java CSVファイルの読み书き的主要内容,如果未能解决你的问题,请参考以下文章
java SuperCSV - Beanを使ってCSVの読み书き操作 - SuperCSV-1.52.jar,univocity-parsers-1.5.6.jar
text 谷歌Chromeでローカルのファイルを読み込み#memo